1 /* boost random/bernoulli_distribution.hpp header file
3 * Copyright Jens Maurer 2000-2001
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: bernoulli_distribution.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
13 * 2001-02-18 moved to individual header files
16 #ifndef BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
17 #define BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
21 #include <boost/random/detail/config.hpp>
26 * Instantiations of class template \bernoulli_distribution model a
27 * \random_distribution. Such a random distribution produces bool values
28 * distributed with probabilities P(true) = p and P(false) = 1-p. p is
29 * the parameter of the distribution.
31 template<class RealType = double>
32 class bernoulli_distribution
35 // In principle, this could work with both integer and floating-point
36 // types. Generating floating-point random numbers in the first
37 // place is probably more expensive, so use integer as input.
38 typedef int input_type;
39 typedef bool result_type;
42 * Constructs a \bernoulli_distribution object.
43 * p is the parameter of the distribution.
45 * Requires: 0 <= p <= 1
47 explicit bernoulli_distribution(const RealType& p_arg = RealType(0.5))
54 // compiler-generated copy ctor and assignment operator are fine
57 * Returns: The "p" parameter of the distribution.
59 RealType p() const { return _p; }
61 * Effects: Subsequent uses of the distribution do not depend
62 * on values produced by any engine prior to invoking reset.
67 * Returns: a random variate distributed according to the
68 * \bernoulli_distribution.
70 template<class Engine>
71 result_type operator()(Engine& eng)
76 return RealType(eng() - (eng.min)()) <= _p * RealType((eng.max)()-(eng.min)());
79 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
81 * Writes the parameters of the distribution to a @c std::ostream.
83 template<class CharT, class Traits>
84 friend std::basic_ostream<CharT,Traits>&
85 operator<<(std::basic_ostream<CharT,Traits>& os, const bernoulli_distribution& bd)
92 * Reads the parameters of the distribution from a @c std::istream.
94 template<class CharT, class Traits>
95 friend std::basic_istream<CharT,Traits>&
96 operator>>(std::basic_istream<CharT,Traits>& is, bernoulli_distribution& bd)
98 is >> std::ws >> bd._p;
109 #endif // BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP