1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/epoc32/include/stdapis/boost/random/binomial_distribution.hpp Tue Mar 16 16:12:26 2010 +0000
1.3 @@ -0,0 +1,81 @@
1.4 +/* boost random/binomial_distribution.hpp header file
1.5 + *
1.6 + * Copyright Jens Maurer 2002
1.7 + * Distributed under the Boost Software License, Version 1.0. (See
1.8 + * accompanying file LICENSE_1_0.txt or copy at
1.9 + * http://www.boost.org/LICENSE_1_0.txt)
1.10 + *
1.11 + * See http://www.boost.org for most recent version including documentation.
1.12 + *
1.13 + * $Id: binomial_distribution.hpp,v 1.10 2005/06/24 22:13:43 jmaurer Exp $
1.14 + *
1.15 + */
1.16 +
1.17 +#ifndef BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP
1.18 +#define BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP
1.19 +
1.20 +#include <cmath>
1.21 +#include <cassert>
1.22 +#include <boost/random/bernoulli_distribution.hpp>
1.23 +
1.24 +namespace boost {
1.25 +
1.26 +// Knuth
1.27 +template<class IntType = int, class RealType = double>
1.28 +class binomial_distribution
1.29 +{
1.30 +public:
1.31 + typedef typename bernoulli_distribution<RealType>::input_type input_type;
1.32 + typedef IntType result_type;
1.33 +
1.34 + explicit binomial_distribution(IntType t = 1,
1.35 + const RealType& p = RealType(0.5))
1.36 + : _bernoulli(p), _t(t)
1.37 + {
1.38 + assert(t >= 0);
1.39 + assert(RealType(0) <= 0 && p <= RealType(1));
1.40 + }
1.41 +
1.42 + // compiler-generated copy ctor and assignment operator are fine
1.43 +
1.44 + IntType t() const { return _t; }
1.45 + RealType p() const { return _bernoulli.p(); }
1.46 + void reset() { }
1.47 +
1.48 + template<class Engine>
1.49 + result_type operator()(Engine& eng)
1.50 + {
1.51 + // TODO: This is O(_t), but it should be O(log(_t)) for large _t
1.52 + result_type n = 0;
1.53 + for(IntType i = 0; i < _t; ++i)
1.54 + if(_bernoulli(eng))
1.55 + ++n;
1.56 + return n;
1.57 + }
1.58 +
1.59 +#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
1.60 + template<class CharT, class Traits>
1.61 + friend std::basic_ostream<CharT,Traits>&
1.62 + operator<<(std::basic_ostream<CharT,Traits>& os, const binomial_distribution& bd)
1.63 + {
1.64 + os << bd._bernoulli << " " << bd._t;
1.65 + return os;
1.66 + }
1.67 +
1.68 + template<class CharT, class Traits>
1.69 + friend std::basic_istream<CharT,Traits>&
1.70 + operator>>(std::basic_istream<CharT,Traits>& is, binomial_distribution& bd)
1.71 + {
1.72 + is >> std::ws >> bd._bernoulli >> std::ws >> bd._t;
1.73 + return is;
1.74 + }
1.75 +#endif
1.76 +
1.77 +private:
1.78 + bernoulli_distribution<RealType> _bernoulli;
1.79 + IntType _t;
1.80 +};
1.81 +
1.82 +} // namespace boost
1.83 +
1.84 +#endif // BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP