1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/epoc32/include/stdapis/boost/random/bernoulli_distribution.hpp Tue Mar 16 16:12:26 2010 +0000
1.3 @@ -0,0 +1,80 @@
1.4 +/* boost random/bernoulli_distribution.hpp header file
1.5 + *
1.6 + * Copyright Jens Maurer 2000-2001
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: bernoulli_distribution.hpp,v 1.19 2004/07/27 03:43:32 dgregor Exp $
1.14 + *
1.15 + * Revision history
1.16 + * 2001-02-18 moved to individual header files
1.17 + */
1.18 +
1.19 +#ifndef BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
1.20 +#define BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
1.21 +
1.22 +#include <cassert>
1.23 +#include <iostream>
1.24 +
1.25 +namespace boost {
1.26 +
1.27 +// Bernoulli distribution: p(true) = p, p(false) = 1-p (boolean)
1.28 +template<class RealType = double>
1.29 +class bernoulli_distribution
1.30 +{
1.31 +public:
1.32 + // In principle, this could work with both integer and floating-point
1.33 + // types. Generating floating-point random numbers in the first
1.34 + // place is probably more expensive, so use integer as input.
1.35 + typedef int input_type;
1.36 + typedef bool result_type;
1.37 +
1.38 + explicit bernoulli_distribution(const RealType& p = RealType(0.5))
1.39 + : _p(p)
1.40 + {
1.41 + assert(p >= 0);
1.42 + assert(p <= 1);
1.43 + }
1.44 +
1.45 + // compiler-generated copy ctor and assignment operator are fine
1.46 +
1.47 + RealType p() const { return _p; }
1.48 + void reset() { }
1.49 +
1.50 + template<class Engine>
1.51 + result_type operator()(Engine& eng)
1.52 + {
1.53 + if(_p == RealType(0))
1.54 + return false;
1.55 + else
1.56 + return RealType(eng() - (eng.min)()) <= _p * RealType((eng.max)()-(eng.min)());
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 bernoulli_distribution& bd)
1.63 + {
1.64 + os << bd._p;
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, bernoulli_distribution& bd)
1.71 + {
1.72 + is >> std::ws >> bd._p;
1.73 + return is;
1.74 + }
1.75 +#endif
1.76 +
1.77 +private:
1.78 + RealType _p;
1.79 +};
1.80 +
1.81 +} // namespace boost
1.82 +
1.83 +#endif // BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP