Chris@16
|
1 /* boost random/bernoulli_distribution.hpp header file
|
Chris@16
|
2 *
|
Chris@16
|
3 * Copyright Jens Maurer 2000-2001
|
Chris@16
|
4 * Copyright Steven Watanabe 2011
|
Chris@16
|
5 * Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
6 * accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
7 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 *
|
Chris@16
|
9 * See http://www.boost.org for most recent version including documentation.
|
Chris@16
|
10 *
|
Chris@101
|
11 * $Id$
|
Chris@16
|
12 *
|
Chris@16
|
13 * Revision history
|
Chris@16
|
14 * 2001-02-18 moved to individual header files
|
Chris@16
|
15 */
|
Chris@16
|
16
|
Chris@16
|
17 #ifndef BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
|
Chris@16
|
18 #define BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
|
Chris@16
|
19
|
Chris@16
|
20 #include <iosfwd>
|
Chris@16
|
21 #include <boost/assert.hpp>
|
Chris@16
|
22 #include <boost/random/detail/config.hpp>
|
Chris@16
|
23 #include <boost/random/detail/operators.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 namespace boost {
|
Chris@16
|
26 namespace random {
|
Chris@16
|
27
|
Chris@16
|
28 /**
|
Chris@16
|
29 * Instantiations of class template \bernoulli_distribution model a
|
Chris@16
|
30 * \random_distribution. Such a random distribution produces bool values
|
Chris@16
|
31 * distributed with probabilities P(true) = p and P(false) = 1-p. p is
|
Chris@16
|
32 * the parameter of the distribution.
|
Chris@16
|
33 */
|
Chris@16
|
34 template<class RealType = double>
|
Chris@16
|
35 class bernoulli_distribution
|
Chris@16
|
36 {
|
Chris@16
|
37 public:
|
Chris@16
|
38 // In principle, this could work with both integer and floating-point
|
Chris@16
|
39 // types. Generating floating-point random numbers in the first
|
Chris@16
|
40 // place is probably more expensive, so use integer as input.
|
Chris@16
|
41 typedef int input_type;
|
Chris@16
|
42 typedef bool result_type;
|
Chris@16
|
43
|
Chris@16
|
44 class param_type
|
Chris@16
|
45 {
|
Chris@16
|
46 public:
|
Chris@16
|
47
|
Chris@16
|
48 typedef bernoulli_distribution distribution_type;
|
Chris@16
|
49
|
Chris@16
|
50 /**
|
Chris@16
|
51 * Constructs the parameters of the distribution.
|
Chris@16
|
52 *
|
Chris@16
|
53 * Requires: 0 <= p <= 1
|
Chris@16
|
54 */
|
Chris@16
|
55 explicit param_type(RealType p_arg = RealType(0.5))
|
Chris@16
|
56 : _p(p_arg)
|
Chris@16
|
57 {
|
Chris@16
|
58 BOOST_ASSERT(_p >= 0);
|
Chris@16
|
59 BOOST_ASSERT(_p <= 1);
|
Chris@16
|
60 }
|
Chris@16
|
61
|
Chris@16
|
62 /** Returns the p parameter of the distribution. */
|
Chris@16
|
63 RealType p() const { return _p; }
|
Chris@16
|
64
|
Chris@16
|
65 /** Writes the parameters to a std::ostream. */
|
Chris@16
|
66 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
|
Chris@16
|
67 {
|
Chris@16
|
68 os << parm._p;
|
Chris@16
|
69 return os;
|
Chris@16
|
70 }
|
Chris@16
|
71
|
Chris@16
|
72 /** Reads the parameters from a std::istream. */
|
Chris@16
|
73 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
|
Chris@16
|
74 {
|
Chris@16
|
75 is >> parm._p;
|
Chris@16
|
76 return is;
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 /** Returns true if the two sets of parameters are equal. */
|
Chris@16
|
80 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
|
Chris@16
|
81 { return lhs._p == rhs._p; }
|
Chris@16
|
82
|
Chris@16
|
83 /** Returns true if the two sets of parameters are different. */
|
Chris@16
|
84 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
|
Chris@16
|
85
|
Chris@16
|
86 private:
|
Chris@16
|
87 RealType _p;
|
Chris@16
|
88 };
|
Chris@16
|
89
|
Chris@16
|
90 /**
|
Chris@16
|
91 * Constructs a \bernoulli_distribution object.
|
Chris@16
|
92 * p is the parameter of the distribution.
|
Chris@16
|
93 *
|
Chris@16
|
94 * Requires: 0 <= p <= 1
|
Chris@16
|
95 */
|
Chris@16
|
96 explicit bernoulli_distribution(const RealType& p_arg = RealType(0.5))
|
Chris@16
|
97 : _p(p_arg)
|
Chris@16
|
98 {
|
Chris@16
|
99 BOOST_ASSERT(_p >= 0);
|
Chris@16
|
100 BOOST_ASSERT(_p <= 1);
|
Chris@16
|
101 }
|
Chris@16
|
102 /**
|
Chris@16
|
103 * Constructs \bernoulli_distribution from its parameters
|
Chris@16
|
104 */
|
Chris@16
|
105 explicit bernoulli_distribution(const param_type& parm)
|
Chris@16
|
106 : _p(parm.p()) {}
|
Chris@16
|
107
|
Chris@16
|
108 // compiler-generated copy ctor and assignment operator are fine
|
Chris@16
|
109
|
Chris@16
|
110 /**
|
Chris@16
|
111 * Returns: The "p" parameter of the distribution.
|
Chris@16
|
112 */
|
Chris@16
|
113 RealType p() const { return _p; }
|
Chris@16
|
114
|
Chris@16
|
115 /** Returns the smallest value that the distribution can produce. */
|
Chris@16
|
116 bool min BOOST_PREVENT_MACRO_SUBSTITUTION () const
|
Chris@16
|
117 { return false; }
|
Chris@16
|
118 /** Returns the largest value that the distribution can produce. */
|
Chris@16
|
119 bool max BOOST_PREVENT_MACRO_SUBSTITUTION () const
|
Chris@16
|
120 { return true; }
|
Chris@16
|
121
|
Chris@16
|
122 /** Returns the parameters of the distribution. */
|
Chris@16
|
123 param_type param() const { return param_type(_p); }
|
Chris@16
|
124 /** Sets the parameters of the distribution. */
|
Chris@16
|
125 void param(const param_type& parm) { _p = parm.p(); }
|
Chris@16
|
126
|
Chris@16
|
127 /**
|
Chris@16
|
128 * Effects: Subsequent uses of the distribution do not depend
|
Chris@16
|
129 * on values produced by any engine prior to invoking reset.
|
Chris@16
|
130 */
|
Chris@16
|
131 void reset() { }
|
Chris@16
|
132
|
Chris@16
|
133 /**
|
Chris@16
|
134 * Returns: a random variate distributed according to the
|
Chris@16
|
135 * \bernoulli_distribution.
|
Chris@16
|
136 */
|
Chris@16
|
137 template<class Engine>
|
Chris@16
|
138 bool operator()(Engine& eng) const
|
Chris@16
|
139 {
|
Chris@16
|
140 if(_p == RealType(0))
|
Chris@16
|
141 return false;
|
Chris@16
|
142 else
|
Chris@16
|
143 return RealType(eng() - (eng.min)()) <= _p * RealType((eng.max)()-(eng.min)());
|
Chris@16
|
144 }
|
Chris@16
|
145
|
Chris@16
|
146 /**
|
Chris@16
|
147 * Returns: a random variate distributed according to the
|
Chris@16
|
148 * \bernoulli_distribution with parameters specified by param.
|
Chris@16
|
149 */
|
Chris@16
|
150 template<class Engine>
|
Chris@16
|
151 bool operator()(Engine& eng, const param_type& parm) const
|
Chris@16
|
152 {
|
Chris@16
|
153 return bernoulli_distribution(parm)(eng);
|
Chris@16
|
154 }
|
Chris@16
|
155
|
Chris@16
|
156 /**
|
Chris@16
|
157 * Writes the parameters of the distribution to a @c std::ostream.
|
Chris@16
|
158 */
|
Chris@16
|
159 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, bernoulli_distribution, bd)
|
Chris@16
|
160 {
|
Chris@16
|
161 os << bd._p;
|
Chris@16
|
162 return os;
|
Chris@16
|
163 }
|
Chris@16
|
164
|
Chris@16
|
165 /**
|
Chris@16
|
166 * Reads the parameters of the distribution from a @c std::istream.
|
Chris@16
|
167 */
|
Chris@16
|
168 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, bernoulli_distribution, bd)
|
Chris@16
|
169 {
|
Chris@16
|
170 is >> bd._p;
|
Chris@16
|
171 return is;
|
Chris@16
|
172 }
|
Chris@16
|
173
|
Chris@16
|
174 /**
|
Chris@16
|
175 * Returns true iff the two distributions will produce identical
|
Chris@16
|
176 * sequences of values given equal generators.
|
Chris@16
|
177 */
|
Chris@16
|
178 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(bernoulli_distribution, lhs, rhs)
|
Chris@16
|
179 { return lhs._p == rhs._p; }
|
Chris@16
|
180
|
Chris@16
|
181 /**
|
Chris@16
|
182 * Returns true iff the two distributions will produce different
|
Chris@16
|
183 * sequences of values given equal generators.
|
Chris@16
|
184 */
|
Chris@16
|
185 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(bernoulli_distribution)
|
Chris@16
|
186
|
Chris@16
|
187 private:
|
Chris@16
|
188 RealType _p;
|
Chris@16
|
189 };
|
Chris@16
|
190
|
Chris@16
|
191 } // namespace random
|
Chris@16
|
192
|
Chris@16
|
193 using random::bernoulli_distribution;
|
Chris@16
|
194
|
Chris@16
|
195 } // namespace boost
|
Chris@16
|
196
|
Chris@16
|
197 #endif // BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
|