| 1 | /* boost random/binomial_distribution.hpp header file | 
|---|
| 2 |  * | 
|---|
| 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) | 
|---|
| 7 |  * | 
|---|
| 8 |  * See http://www.boost.org for most recent version including documentation. | 
|---|
| 9 |  * | 
|---|
| 10 |  * $Id: binomial_distribution.hpp,v 1.10 2005/06/24 22:13:43 jmaurer Exp $ | 
|---|
| 11 |  * | 
|---|
| 12 |  */ | 
|---|
| 13 |  | 
|---|
| 14 | #ifndef BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP | 
|---|
| 15 | #define BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP | 
|---|
| 16 |  | 
|---|
| 17 | #include <cmath> | 
|---|
| 18 | #include <cassert> | 
|---|
| 19 | #include <boost/random/bernoulli_distribution.hpp> | 
|---|
| 20 |  | 
|---|
| 21 | namespace boost { | 
|---|
| 22 |  | 
|---|
| 23 | // Knuth | 
|---|
| 24 | template<class IntType = int, class RealType = double> | 
|---|
| 25 | class binomial_distribution | 
|---|
| 26 | { | 
|---|
| 27 | public: | 
|---|
| 28 |   typedef typename bernoulli_distribution<RealType>::input_type input_type; | 
|---|
| 29 |   typedef IntType result_type; | 
|---|
| 30 |  | 
|---|
| 31 |   explicit binomial_distribution(IntType t = 1, | 
|---|
| 32 |                                  const RealType& p = RealType(0.5)) | 
|---|
| 33 |     : _bernoulli(p), _t(t) | 
|---|
| 34 |   { | 
|---|
| 35 |     assert(t >= 0); | 
|---|
| 36 |     assert(RealType(0) <= 0 && p <= RealType(1)); | 
|---|
| 37 |   } | 
|---|
| 38 |  | 
|---|
| 39 |   // compiler-generated copy ctor and assignment operator are fine | 
|---|
| 40 |  | 
|---|
| 41 |   IntType t() const { return _t; } | 
|---|
| 42 |   RealType p() const { return _bernoulli.p(); } | 
|---|
| 43 |   void reset() { } | 
|---|
| 44 |  | 
|---|
| 45 |   template<class Engine> | 
|---|
| 46 |   result_type operator()(Engine& eng) | 
|---|
| 47 |   { | 
|---|
| 48 |     // TODO: This is O(_t), but it should be O(log(_t)) for large _t | 
|---|
| 49 |     result_type n = 0; | 
|---|
| 50 |     for(IntType i = 0; i < _t; ++i) | 
|---|
| 51 |       if(_bernoulli(eng)) | 
|---|
| 52 |         ++n; | 
|---|
| 53 |     return n; | 
|---|
| 54 |   } | 
|---|
| 55 |  | 
|---|
| 56 | #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) | 
|---|
| 57 |   template<class CharT, class Traits> | 
|---|
| 58 |   friend std::basic_ostream<CharT,Traits>& | 
|---|
| 59 |   operator<<(std::basic_ostream<CharT,Traits>& os, const binomial_distribution& bd) | 
|---|
| 60 |   { | 
|---|
| 61 |     os << bd._bernoulli << " " << bd._t; | 
|---|
| 62 |     return os; | 
|---|
| 63 |   } | 
|---|
| 64 |  | 
|---|
| 65 |   template<class CharT, class Traits> | 
|---|
| 66 |   friend std::basic_istream<CharT,Traits>& | 
|---|
| 67 |   operator>>(std::basic_istream<CharT,Traits>& is, binomial_distribution& bd) | 
|---|
| 68 |   { | 
|---|
| 69 |     is >> std::ws >> bd._bernoulli >> std::ws >> bd._t; | 
|---|
| 70 |     return is; | 
|---|
| 71 |   } | 
|---|
| 72 | #endif | 
|---|
| 73 |  | 
|---|
| 74 | private: | 
|---|
| 75 |   bernoulli_distribution<RealType> _bernoulli; | 
|---|
| 76 |   IntType _t; | 
|---|
| 77 | }; | 
|---|
| 78 |  | 
|---|
| 79 | } // namespace boost | 
|---|
| 80 |  | 
|---|
| 81 | #endif // BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP | 
|---|