sl@0: /* boost random/poisson_distribution.hpp header file sl@0: * sl@0: * Copyright Jens Maurer 2002 sl@0: * Distributed under the Boost Software License, Version 1.0. (See sl@0: * accompanying file LICENSE_1_0.txt or copy at sl@0: * http://www.boost.org/LICENSE_1_0.txt) sl@0: * sl@0: * See http://www.boost.org for most recent version including documentation. sl@0: * sl@0: * $Id: poisson_distribution.hpp,v 1.15 2005/05/21 15:57:00 dgregor Exp $ sl@0: * sl@0: */ sl@0: sl@0: #ifndef BOOST_RANDOM_POISSON_DISTRIBUTION_HPP sl@0: #define BOOST_RANDOM_POISSON_DISTRIBUTION_HPP sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: namespace boost { sl@0: sl@0: // Knuth sl@0: template sl@0: class poisson_distribution sl@0: { sl@0: public: sl@0: typedef RealType input_type; sl@0: typedef IntType result_type; sl@0: sl@0: explicit poisson_distribution(const RealType& mean = RealType(1)) sl@0: : _mean(mean) sl@0: { sl@0: #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS sl@0: // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope sl@0: BOOST_STATIC_ASSERT(std::numeric_limits::is_integer); sl@0: BOOST_STATIC_ASSERT(!std::numeric_limits::is_integer); sl@0: #endif sl@0: sl@0: assert(mean > RealType(0)); sl@0: init(); sl@0: } sl@0: sl@0: // compiler-generated copy ctor and assignment operator are fine sl@0: sl@0: RealType mean() const { return _mean; } sl@0: void reset() { } sl@0: sl@0: template sl@0: result_type operator()(Engine& eng) sl@0: { sl@0: // TODO: This is O(_mean), but it should be O(log(_mean)) for large _mean sl@0: RealType product = RealType(1); sl@0: for(result_type m = 0; ; ++m) { sl@0: product *= eng(); sl@0: if(product <= _exp_mean) sl@0: return m; sl@0: } sl@0: } sl@0: sl@0: #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) sl@0: template sl@0: friend std::basic_ostream& sl@0: operator<<(std::basic_ostream& os, const poisson_distribution& pd) sl@0: { sl@0: os << pd._mean; sl@0: return os; sl@0: } sl@0: sl@0: template sl@0: friend std::basic_istream& sl@0: operator>>(std::basic_istream& is, poisson_distribution& pd) sl@0: { sl@0: is >> std::ws >> pd._mean; sl@0: pd.init(); sl@0: return is; sl@0: } sl@0: #endif sl@0: sl@0: private: sl@0: void init() sl@0: { sl@0: #ifndef BOOST_NO_STDC_NAMESPACE sl@0: // allow for Koenig lookup sl@0: using std::exp; sl@0: #endif sl@0: _exp_mean = exp(-_mean); sl@0: } sl@0: sl@0: RealType _mean; sl@0: // some precomputed data from the parameters sl@0: RealType _exp_mean; sl@0: }; sl@0: sl@0: } // namespace boost sl@0: sl@0: #endif // BOOST_RANDOM_POISSON_DISTRIBUTION_HPP