1 /* boost random/binomial_distribution.hpp header file
\r
3 * Copyright Jens Maurer 2002
\r
4 * Distributed under the Boost Software License, Version 1.0. (See
\r
5 * accompanying file LICENSE_1_0.txt or copy at
\r
6 * http://www.boost.org/LICENSE_1_0.txt)
\r
8 * See http://www.boost.org for most recent version including documentation.
\r
10 * $Id: binomial_distribution.hpp 52492 2009-04-19 14:55:57Z steven_watanabe $
\r
14 #ifndef BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP
\r
15 #define BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP
\r
17 #include <boost/config/no_tr1/cmath.hpp>
\r
19 #include <boost/random/detail/config.hpp>
\r
20 #include <boost/random/bernoulli_distribution.hpp>
\r
25 template<class IntType = int, class RealType = double>
\r
26 class binomial_distribution
\r
29 typedef typename bernoulli_distribution<RealType>::input_type input_type;
\r
30 typedef IntType result_type;
\r
32 explicit binomial_distribution(IntType t_arg = 1,
\r
33 const RealType& p_arg = RealType(0.5))
\r
34 : _bernoulli(p_arg), _t(t_arg)
\r
37 assert(RealType(0) <= p_arg && p_arg <= RealType(1));
\r
40 // compiler-generated copy ctor and assignment operator are fine
\r
42 IntType t() const { return _t; }
\r
43 RealType p() const { return _bernoulli.p(); }
\r
46 template<class Engine>
\r
47 result_type operator()(Engine& eng)
\r
49 // TODO: This is O(_t), but it should be O(log(_t)) for large _t
\r
51 for(IntType i = 0; i < _t; ++i)
\r
57 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
\r
58 template<class CharT, class Traits>
\r
59 friend std::basic_ostream<CharT,Traits>&
\r
60 operator<<(std::basic_ostream<CharT,Traits>& os, const binomial_distribution& bd)
\r
62 os << bd._bernoulli << " " << bd._t;
\r
66 template<class CharT, class Traits>
\r
67 friend std::basic_istream<CharT,Traits>&
\r
68 operator>>(std::basic_istream<CharT,Traits>& is, binomial_distribution& bd)
\r
70 is >> std::ws >> bd._bernoulli >> std::ws >> bd._t;
\r
76 bernoulli_distribution<RealType> _bernoulli;
\r
80 } // namespace boost
\r
82 #endif // BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP
\r