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.
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>
25 // Bernoulli distribution: p(true) = p, p(false) = 1-p (boolean)
26 template<class RealType
= double>
27 class bernoulli_distribution
30 // In principle, this could work with both integer and floating-point
31 // types. Generating floating-point random numbers in the first
32 // place is probably more expensive, so use integer as input.
33 typedef int input_type
;
34 typedef bool result_type
;
36 explicit bernoulli_distribution(const RealType
& p_arg
= RealType(0.5))
43 // compiler-generated copy ctor and assignment operator are fine
45 RealType
p() const { return _p
; }
48 template<class Engine
>
49 result_type
operator()(Engine
& eng
)
54 return RealType(eng() - (eng
.min
)()) <= _p
* RealType((eng
.max
)()-(eng
.min
)());
57 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
58 template<class CharT
, class Traits
>
59 friend std::basic_ostream
<CharT
,Traits
>&
60 operator<<(std::basic_ostream
<CharT
,Traits
>& os
, const bernoulli_distribution
& bd
)
66 template<class CharT
, class Traits
>
67 friend std::basic_istream
<CharT
,Traits
>&
68 operator>>(std::basic_istream
<CharT
,Traits
>& is
, bernoulli_distribution
& bd
)
70 is
>> std::ws
>> bd
._p
;
81 #endif // BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP