1 /* boost random/bernoulli_distribution.hpp header file
\r
3 * Copyright Jens Maurer 2000-2001
\r
4 * Distributed under the Boost Software License, Version 1.0. (See
\r
5 * accompanying file LICENSE_1_0.txt or copy at
\r
6 * http://www.boost.org/LICENSE_1_0.txt)
\r
8 * See http://www.boost.org for most recent version including documentation.
\r
10 * $Id: bernoulli_distribution.hpp 52492 2009-04-19 14:55:57Z steven_watanabe $
\r
13 * 2001-02-18 moved to individual header files
\r
16 #ifndef BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
\r
17 #define BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
\r
21 #include <boost/random/detail/config.hpp>
\r
25 // Bernoulli distribution: p(true) = p, p(false) = 1-p (boolean)
\r
26 template<class RealType = double>
\r
27 class bernoulli_distribution
\r
30 // In principle, this could work with both integer and floating-point
\r
31 // types. Generating floating-point random numbers in the first
\r
32 // place is probably more expensive, so use integer as input.
\r
33 typedef int input_type;
\r
34 typedef bool result_type;
\r
36 explicit bernoulli_distribution(const RealType& p_arg = RealType(0.5))
\r
43 // compiler-generated copy ctor and assignment operator are fine
\r
45 RealType p() const { return _p; }
\r
48 template<class Engine>
\r
49 result_type operator()(Engine& eng)
\r
51 if(_p == RealType(0))
\r
54 return RealType(eng() - (eng.min)()) <= _p * RealType((eng.max)()-(eng.min)());
\r
57 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
\r
58 template<class CharT, class Traits>
\r
59 friend std::basic_ostream<CharT,Traits>&
\r
60 operator<<(std::basic_ostream<CharT,Traits>& os, const bernoulli_distribution& bd)
\r
66 template<class CharT, class Traits>
\r
67 friend std::basic_istream<CharT,Traits>&
\r
68 operator>>(std::basic_istream<CharT,Traits>& is, bernoulli_distribution& bd)
\r
70 is >> std::ws >> bd._p;
\r
79 } // namespace boost
\r
81 #endif // BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
\r