sl@0: /* boost random/binomial_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: binomial_distribution.hpp,v 1.10 2005/06/24 22:13:43 jmaurer Exp $ sl@0: * sl@0: */ sl@0: sl@0: #ifndef BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP sl@0: #define BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP sl@0: 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 binomial_distribution sl@0: { sl@0: public: sl@0: typedef typename bernoulli_distribution::input_type input_type; sl@0: typedef IntType result_type; sl@0: sl@0: explicit binomial_distribution(IntType t = 1, sl@0: const RealType& p = RealType(0.5)) sl@0: : _bernoulli(p), _t(t) sl@0: { sl@0: assert(t >= 0); sl@0: assert(RealType(0) <= 0 && p <= RealType(1)); sl@0: } sl@0: sl@0: // compiler-generated copy ctor and assignment operator are fine sl@0: sl@0: IntType t() const { return _t; } sl@0: RealType p() const { return _bernoulli.p(); } sl@0: void reset() { } sl@0: sl@0: template sl@0: result_type operator()(Engine& eng) sl@0: { sl@0: // TODO: This is O(_t), but it should be O(log(_t)) for large _t sl@0: result_type n = 0; sl@0: for(IntType i = 0; i < _t; ++i) sl@0: if(_bernoulli(eng)) sl@0: ++n; sl@0: return n; 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 binomial_distribution& bd) sl@0: { sl@0: os << bd._bernoulli << " " << bd._t; sl@0: return os; sl@0: } sl@0: sl@0: template sl@0: friend std::basic_istream& sl@0: operator>>(std::basic_istream& is, binomial_distribution& bd) sl@0: { sl@0: is >> std::ws >> bd._bernoulli >> std::ws >> bd._t; sl@0: return is; sl@0: } sl@0: #endif sl@0: sl@0: private: sl@0: bernoulli_distribution _bernoulli; sl@0: IntType _t; sl@0: }; sl@0: sl@0: } // namespace boost sl@0: sl@0: #endif // BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP