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.
14 #ifndef BOOST_RANDOM_POISSON_DISTRIBUTION_HPP
15 #define BOOST_RANDOM_POISSON_DISTRIBUTION_HPP
17 #include <boost/config/no_tr1/cmath.hpp>
20 #include <boost/limits.hpp>
21 #include <boost/static_assert.hpp>
22 #include <boost/random/detail/config.hpp>
27 template<class IntType
= int, class RealType
= double>
28 class poisson_distribution
31 typedef RealType input_type
;
32 typedef IntType result_type
;
34 explicit poisson_distribution(const RealType
& mean_arg
= RealType(1))
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
);
43 assert(_mean
> RealType(0));
47 // compiler-generated copy ctor and assignment operator are fine
49 RealType
mean() const { return _mean
; }
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
) {
59 if(product
<= _exp_mean
)
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
)
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
;
86 #ifndef BOOST_NO_STDC_NAMESPACE
87 // allow for Koenig lookup
90 _exp_mean
= exp(-_mean
);
94 // some precomputed data from the parameters
100 #endif // BOOST_RANDOM_POISSON_DISTRIBUTION_HPP