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
20 #include <boost/limits.hpp>
21 #include <boost/static_assert.hpp>
26 template<class IntType
= int, class RealType
= double>
27 class poisson_distribution
30 typedef RealType input_type
;
31 typedef IntType result_type
;
33 explicit poisson_distribution(const RealType
& mean
= RealType(1))
36 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
37 // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
38 BOOST_STATIC_ASSERT(std::numeric_limits
<IntType
>::is_integer
);
39 BOOST_STATIC_ASSERT(!std::numeric_limits
<RealType
>::is_integer
);
42 assert(mean
> RealType(0));
46 // compiler-generated copy ctor and assignment operator are fine
48 RealType
mean() const { return _mean
; }
51 template<class Engine
>
52 result_type
operator()(Engine
& eng
)
54 // TODO: This is O(_mean), but it should be O(log(_mean)) for large _mean
55 RealType product
= RealType(1);
56 for(result_type m
= 0; ; ++m
) {
58 if(product
<= _exp_mean
)
63 #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
64 template<class CharT
, class Traits
>
65 friend std::basic_ostream
<CharT
,Traits
>&
66 operator<<(std::basic_ostream
<CharT
,Traits
>& os
, const poisson_distribution
& pd
)
72 template<class CharT
, class Traits
>
73 friend std::basic_istream
<CharT
,Traits
>&
74 operator>>(std::basic_istream
<CharT
,Traits
>& is
, poisson_distribution
& pd
)
76 is
>> std::ws
>> pd
._mean
;
85 #ifndef BOOST_NO_STDC_NAMESPACE
86 // allow for Koenig lookup
89 _exp_mean
= exp(-_mean
);
93 // some precomputed data from the parameters
99 #endif // BOOST_RANDOM_POISSON_DISTRIBUTION_HPP