fix doc example typo
[boost.git] / boost / random / poisson_distribution.hpp
blobe6208aab2f3c2cfc47925ac1dc7ade7a49359f4c
1 /* boost random/poisson_distribution.hpp header file
3 * Copyright Jens Maurer 2002
4 * Distributed under the Boost Software License, Version 1.0. (See
5 * accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
8 * See http://www.boost.org for most recent version including documentation.
10 * $Id$
14 #ifndef BOOST_RANDOM_POISSON_DISTRIBUTION_HPP
15 #define BOOST_RANDOM_POISSON_DISTRIBUTION_HPP
17 #include <boost/config/no_tr1/cmath.hpp>
18 #include <cassert>
19 #include <iostream>
20 #include <boost/limits.hpp>
21 #include <boost/static_assert.hpp>
22 #include <boost/random/detail/config.hpp>
24 namespace boost {
26 // Knuth
27 template<class IntType = int, class RealType = double>
28 class poisson_distribution
30 public:
31 typedef RealType input_type;
32 typedef IntType result_type;
34 explicit poisson_distribution(const RealType& mean_arg = RealType(1))
35 : _mean(mean_arg)
37 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
38 // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
39 BOOST_STATIC_ASSERT(std::numeric_limits<IntType>::is_integer);
40 BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
41 #endif
43 assert(_mean > RealType(0));
44 init();
47 // compiler-generated copy ctor and assignment operator are fine
49 RealType mean() const { return _mean; }
50 void reset() { }
52 template<class Engine>
53 result_type operator()(Engine& eng)
55 // TODO: This is O(_mean), but it should be O(log(_mean)) for large _mean
56 RealType product = RealType(1);
57 for(result_type m = 0; ; ++m) {
58 product *= eng();
59 if(product <= _exp_mean)
60 return m;
64 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
65 template<class CharT, class Traits>
66 friend std::basic_ostream<CharT,Traits>&
67 operator<<(std::basic_ostream<CharT,Traits>& os, const poisson_distribution& pd)
69 os << pd._mean;
70 return os;
73 template<class CharT, class Traits>
74 friend std::basic_istream<CharT,Traits>&
75 operator>>(std::basic_istream<CharT,Traits>& is, poisson_distribution& pd)
77 is >> std::ws >> pd._mean;
78 pd.init();
79 return is;
81 #endif
83 private:
84 void init()
86 #ifndef BOOST_NO_STDC_NAMESPACE
87 // allow for Koenig lookup
88 using std::exp;
89 #endif
90 _exp_mean = exp(-_mean);
93 RealType _mean;
94 // some precomputed data from the parameters
95 RealType _exp_mean;
98 } // namespace boost
100 #endif // BOOST_RANDOM_POISSON_DISTRIBUTION_HPP