1 /* boost random/binomial_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: binomial_distribution.hpp,v 1.10 2005/06/24 22:13:43 jmaurer Exp $
14 #ifndef BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP
15 #define BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP
19 #include <boost/random/bernoulli_distribution.hpp>
24 template<class IntType = int, class RealType = double>
25 class binomial_distribution
28 typedef typename bernoulli_distribution<RealType>::input_type input_type;
29 typedef IntType result_type;
31 explicit binomial_distribution(IntType t = 1,
32 const RealType& p = RealType(0.5))
33 : _bernoulli(p), _t(t)
36 assert(RealType(0) <= 0 && p <= RealType(1));
39 // compiler-generated copy ctor and assignment operator are fine
41 IntType t() const { return _t; }
42 RealType p() const { return _bernoulli.p(); }
45 template<class Engine>
46 result_type operator()(Engine& eng)
48 // TODO: This is O(_t), but it should be O(log(_t)) for large _t
50 for(IntType i = 0; i < _t; ++i)
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)
61 os << bd._bernoulli << " " << bd._t;
65 template<class CharT, class Traits>
66 friend std::basic_istream<CharT,Traits>&
67 operator>>(std::basic_istream<CharT,Traits>& is, binomial_distribution& bd)
69 is >> std::ws >> bd._bernoulli >> std::ws >> bd._t;
75 bernoulli_distribution<RealType> _bernoulli;
81 #endif // BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP