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: poisson_distribution.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
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>
29 * An instantiation of the class template @c poisson_distribution is a
30 * model of \random_distribution. The poisson distribution has
31 * \f$p(i) = \frac{e^{-\lambda}\lambda^i}{i!}\f$
33 template<class IntType = int, class RealType = double>
34 class poisson_distribution
37 typedef RealType input_type;
38 typedef IntType result_type;
41 * Constructs a @c poisson_distribution with the parameter @c mean.
45 explicit poisson_distribution(const RealType& mean_arg = RealType(1))
48 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
49 // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
50 BOOST_STATIC_ASSERT(std::numeric_limits<IntType>::is_integer);
51 BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
54 assert(_mean > RealType(0));
58 // compiler-generated copy ctor and assignment operator are fine
61 * Returns: the "mean" parameter of the distribution.
63 RealType mean() const { return _mean; }
66 template<class Engine>
67 result_type operator()(Engine& eng)
69 // TODO: This is O(_mean), but it should be O(log(_mean)) for large _mean
70 RealType product = RealType(1);
71 for(result_type m = 0; ; ++m) {
73 if(product <= _exp_mean)
78 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
79 template<class CharT, class Traits>
80 friend std::basic_ostream<CharT,Traits>&
81 operator<<(std::basic_ostream<CharT,Traits>& os, const poisson_distribution& pd)
87 template<class CharT, class Traits>
88 friend std::basic_istream<CharT,Traits>&
89 operator>>(std::basic_istream<CharT,Traits>& is, poisson_distribution& pd)
91 is >> std::ws >> pd._mean;
98 /// \cond hide_private_members
101 #ifndef BOOST_NO_STDC_NAMESPACE
102 // allow for Koenig lookup
105 _exp_mean = exp(-_mean);
110 // some precomputed data from the parameters
116 #endif // BOOST_RANDOM_POISSON_DISTRIBUTION_HPP