Chris@16: /* boost random/bernoulli_distribution.hpp header file Chris@16: * Chris@16: * Copyright Jens Maurer 2000-2001 Chris@16: * Copyright Steven Watanabe 2011 Chris@16: * Distributed under the Boost Software License, Version 1.0. (See Chris@16: * accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@16: * See http://www.boost.org for most recent version including documentation. Chris@16: * Chris@101: * $Id$ Chris@16: * Chris@16: * Revision history Chris@16: * 2001-02-18 moved to individual header files Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP Chris@16: #define BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace random { Chris@16: Chris@16: /** Chris@16: * Instantiations of class template \bernoulli_distribution model a Chris@16: * \random_distribution. Such a random distribution produces bool values Chris@16: * distributed with probabilities P(true) = p and P(false) = 1-p. p is Chris@16: * the parameter of the distribution. Chris@16: */ Chris@16: template Chris@16: class bernoulli_distribution Chris@16: { Chris@16: public: Chris@16: // In principle, this could work with both integer and floating-point Chris@16: // types. Generating floating-point random numbers in the first Chris@16: // place is probably more expensive, so use integer as input. Chris@16: typedef int input_type; Chris@16: typedef bool result_type; Chris@16: Chris@16: class param_type Chris@16: { Chris@16: public: Chris@16: Chris@16: typedef bernoulli_distribution distribution_type; Chris@16: Chris@16: /** Chris@16: * Constructs the parameters of the distribution. Chris@16: * Chris@16: * Requires: 0 <= p <= 1 Chris@16: */ Chris@16: explicit param_type(RealType p_arg = RealType(0.5)) Chris@16: : _p(p_arg) Chris@16: { Chris@16: BOOST_ASSERT(_p >= 0); Chris@16: BOOST_ASSERT(_p <= 1); Chris@16: } Chris@16: Chris@16: /** Returns the p parameter of the distribution. */ Chris@16: RealType p() const { return _p; } Chris@16: Chris@16: /** Writes the parameters to a std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm) Chris@16: { Chris@16: os << parm._p; Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads the parameters from a std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm) Chris@16: { Chris@16: is >> parm._p; Chris@16: return is; Chris@16: } Chris@16: Chris@16: /** Returns true if the two sets of parameters are equal. */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs) Chris@16: { return lhs._p == rhs._p; } Chris@16: Chris@16: /** Returns true if the two sets of parameters are different. */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type) Chris@16: Chris@16: private: Chris@16: RealType _p; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * Constructs a \bernoulli_distribution object. Chris@16: * p is the parameter of the distribution. Chris@16: * Chris@16: * Requires: 0 <= p <= 1 Chris@16: */ Chris@16: explicit bernoulli_distribution(const RealType& p_arg = RealType(0.5)) Chris@16: : _p(p_arg) Chris@16: { Chris@16: BOOST_ASSERT(_p >= 0); Chris@16: BOOST_ASSERT(_p <= 1); Chris@16: } Chris@16: /** Chris@16: * Constructs \bernoulli_distribution from its parameters Chris@16: */ Chris@16: explicit bernoulli_distribution(const param_type& parm) Chris@16: : _p(parm.p()) {} Chris@16: Chris@16: // compiler-generated copy ctor and assignment operator are fine Chris@16: Chris@16: /** Chris@16: * Returns: The "p" parameter of the distribution. Chris@16: */ Chris@16: RealType p() const { return _p; } Chris@16: Chris@16: /** Returns the smallest value that the distribution can produce. */ Chris@16: bool min BOOST_PREVENT_MACRO_SUBSTITUTION () const Chris@16: { return false; } Chris@16: /** Returns the largest value that the distribution can produce. */ Chris@16: bool max BOOST_PREVENT_MACRO_SUBSTITUTION () const Chris@16: { return true; } Chris@16: Chris@16: /** Returns the parameters of the distribution. */ Chris@16: param_type param() const { return param_type(_p); } Chris@16: /** Sets the parameters of the distribution. */ Chris@16: void param(const param_type& parm) { _p = parm.p(); } Chris@16: Chris@16: /** Chris@16: * Effects: Subsequent uses of the distribution do not depend Chris@16: * on values produced by any engine prior to invoking reset. Chris@16: */ Chris@16: void reset() { } Chris@16: Chris@16: /** Chris@16: * Returns: a random variate distributed according to the Chris@16: * \bernoulli_distribution. Chris@16: */ Chris@16: template Chris@16: bool operator()(Engine& eng) const Chris@16: { Chris@16: if(_p == RealType(0)) Chris@16: return false; Chris@16: else Chris@16: return RealType(eng() - (eng.min)()) <= _p * RealType((eng.max)()-(eng.min)()); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns: a random variate distributed according to the Chris@16: * \bernoulli_distribution with parameters specified by param. Chris@16: */ Chris@16: template Chris@16: bool operator()(Engine& eng, const param_type& parm) const Chris@16: { Chris@16: return bernoulli_distribution(parm)(eng); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Writes the parameters of the distribution to a @c std::ostream. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, bernoulli_distribution, bd) Chris@16: { Chris@16: os << bd._p; Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Reads the parameters of the distribution from a @c std::istream. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, bernoulli_distribution, bd) Chris@16: { Chris@16: is >> bd._p; Chris@16: return is; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns true iff the two distributions will produce identical Chris@16: * sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(bernoulli_distribution, lhs, rhs) Chris@16: { return lhs._p == rhs._p; } Chris@16: Chris@16: /** Chris@16: * Returns true iff the two distributions will produce different Chris@16: * sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(bernoulli_distribution) Chris@16: Chris@16: private: Chris@16: RealType _p; Chris@16: }; Chris@16: Chris@16: } // namespace random Chris@16: Chris@16: using random::bernoulli_distribution; Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP