Chris@16
|
1 /* boost random/uniform_smallint.hpp header file
|
Chris@16
|
2 *
|
Chris@16
|
3 * Copyright Jens Maurer 2000-2001
|
Chris@16
|
4 * Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
5 * accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 *
|
Chris@16
|
8 * See http://www.boost.org for most recent version including documentation.
|
Chris@16
|
9 *
|
Chris@101
|
10 * $Id$
|
Chris@16
|
11 *
|
Chris@16
|
12 * Revision history
|
Chris@16
|
13 * 2001-04-08 added min<max assertion (N. Becker)
|
Chris@16
|
14 * 2001-02-18 moved to individual header files
|
Chris@16
|
15 */
|
Chris@16
|
16
|
Chris@16
|
17 #ifndef BOOST_RANDOM_UNIFORM_SMALLINT_HPP
|
Chris@16
|
18 #define BOOST_RANDOM_UNIFORM_SMALLINT_HPP
|
Chris@16
|
19
|
Chris@16
|
20 #include <istream>
|
Chris@16
|
21 #include <iosfwd>
|
Chris@16
|
22 #include <boost/assert.hpp>
|
Chris@16
|
23 #include <boost/config.hpp>
|
Chris@16
|
24 #include <boost/limits.hpp>
|
Chris@16
|
25 #include <boost/type_traits/is_integral.hpp>
|
Chris@16
|
26 #include <boost/random/detail/config.hpp>
|
Chris@16
|
27 #include <boost/random/detail/operators.hpp>
|
Chris@16
|
28 #include <boost/random/detail/signed_unsigned_tools.hpp>
|
Chris@16
|
29 #include <boost/random/uniform_01.hpp>
|
Chris@16
|
30 #include <boost/detail/workaround.hpp>
|
Chris@101
|
31 #include <boost/mpl/bool.hpp>
|
Chris@16
|
32
|
Chris@16
|
33 namespace boost {
|
Chris@16
|
34 namespace random {
|
Chris@16
|
35
|
Chris@16
|
36 // uniform integer distribution on a small range [min, max]
|
Chris@16
|
37
|
Chris@16
|
38 /**
|
Chris@16
|
39 * The distribution function uniform_smallint models a \random_distribution.
|
Chris@16
|
40 * On each invocation, it returns a random integer value uniformly distributed
|
Chris@16
|
41 * in the set of integer numbers {min, min+1, min+2, ..., max}. It assumes
|
Chris@16
|
42 * that the desired range (max-min+1) is small compared to the range of the
|
Chris@16
|
43 * underlying source of random numbers and thus makes no attempt to limit
|
Chris@16
|
44 * quantization errors.
|
Chris@16
|
45 *
|
Chris@16
|
46 * Let \f$r_{\mathtt{out}} = (\mbox{max}-\mbox{min}+1)\f$ the desired range of
|
Chris@16
|
47 * integer numbers, and
|
Chris@16
|
48 * let \f$r_{\mathtt{base}}\f$ be the range of the underlying source of random
|
Chris@16
|
49 * numbers. Then, for the uniform distribution, the theoretical probability
|
Chris@16
|
50 * for any number i in the range \f$r_{\mathtt{out}}\f$ will be
|
Chris@16
|
51 * \f$\displaystyle p_{\mathtt{out}}(i) = \frac{1}{r_{\mathtt{out}}}\f$.
|
Chris@16
|
52 * Likewise, assume a uniform distribution on \f$r_{\mathtt{base}}\f$ for
|
Chris@16
|
53 * the underlying source of random numbers, i.e.
|
Chris@16
|
54 * \f$\displaystyle p_{\mathtt{base}}(i) = \frac{1}{r_{\mathtt{base}}}\f$.
|
Chris@16
|
55 * Let \f$p_{\mathtt{out\_s}}(i)\f$ denote the random
|
Chris@16
|
56 * distribution generated by @c uniform_smallint. Then the sum over all
|
Chris@16
|
57 * i in \f$r_{\mathtt{out}}\f$ of
|
Chris@16
|
58 * \f$\displaystyle
|
Chris@16
|
59 * \left(\frac{p_{\mathtt{out\_s}}(i)}{p_{\mathtt{out}}(i)} - 1\right)^2\f$
|
Chris@16
|
60 * shall not exceed
|
Chris@16
|
61 * \f$\displaystyle \frac{r_{\mathtt{out}}}{r_{\mathtt{base}}^2}
|
Chris@16
|
62 * (r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}})
|
Chris@16
|
63 * (r_{\mathtt{out}} - r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}})\f$.
|
Chris@16
|
64 *
|
Chris@16
|
65 * The template parameter IntType shall denote an integer-like value type.
|
Chris@16
|
66 *
|
Chris@16
|
67 * @xmlnote
|
Chris@16
|
68 * The property above is the square sum of the relative differences
|
Chris@16
|
69 * in probabilities between the desired uniform distribution
|
Chris@16
|
70 * \f$p_{\mathtt{out}}(i)\f$ and the generated distribution
|
Chris@16
|
71 * \f$p_{\mathtt{out\_s}}(i)\f$.
|
Chris@16
|
72 * The property can be fulfilled with the calculation
|
Chris@16
|
73 * \f$(\mbox{base\_rng} \mbox{ mod } r_{\mathtt{out}})\f$, as follows:
|
Chris@16
|
74 * Let \f$r = r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}}\f$.
|
Chris@16
|
75 * The base distribution on \f$r_{\mathtt{base}}\f$ is folded onto the
|
Chris@16
|
76 * range \f$r_{\mathtt{out}}\f$. The numbers i < r have assigned
|
Chris@16
|
77 * \f$\displaystyle
|
Chris@16
|
78 * \left\lfloor\frac{r_{\mathtt{base}}}{r_{\mathtt{out}}}\right\rfloor+1\f$
|
Chris@16
|
79 * numbers of the base distribution, the rest has only \f$\displaystyle
|
Chris@16
|
80 * \left\lfloor\frac{r_{\mathtt{base}}}{r_{\mathtt{out}}}\right\rfloor\f$.
|
Chris@16
|
81 * Therefore,
|
Chris@16
|
82 * \f$\displaystyle p_{\mathtt{out\_s}}(i) =
|
Chris@16
|
83 * \left(\left\lfloor\frac{r_{\mathtt{base}}}
|
Chris@16
|
84 * {r_{\mathtt{out}}}\right\rfloor+1\right) /
|
Chris@16
|
85 * r_{\mathtt{base}}\f$ for i < r and \f$\displaystyle p_{\mathtt{out\_s}}(i) =
|
Chris@16
|
86 * \left\lfloor\frac{r_{\mathtt{base}}}
|
Chris@16
|
87 * {r_{\mathtt{out}}}\right\rfloor/r_{\mathtt{base}}\f$ otherwise.
|
Chris@16
|
88 * Substituting this in the
|
Chris@16
|
89 * above sum formula leads to the desired result.
|
Chris@16
|
90 * @endxmlnote
|
Chris@16
|
91 *
|
Chris@16
|
92 * Note: The upper bound for
|
Chris@16
|
93 * \f$(r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}})
|
Chris@16
|
94 * (r_{\mathtt{out}} - r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}})\f$ is
|
Chris@16
|
95 * \f$\displaystyle \frac{r_{\mathtt{out}}^2}{4}\f$. Regarding the upper bound
|
Chris@16
|
96 * for the square sum of the relative quantization error of
|
Chris@16
|
97 * \f$\displaystyle \frac{r_\mathtt{out}^3}{4r_{\mathtt{base}}^2}\f$, it
|
Chris@16
|
98 * seems wise to either choose \f$r_{\mathtt{base}}\f$ so that
|
Chris@16
|
99 * \f$r_{\mathtt{base}} > 10r_{\mathtt{out}}^2\f$ or ensure that
|
Chris@16
|
100 * \f$r_{\mathtt{base}}\f$ is
|
Chris@16
|
101 * divisible by \f$r_{\mathtt{out}}\f$.
|
Chris@16
|
102 */
|
Chris@16
|
103 template<class IntType = int>
|
Chris@16
|
104 class uniform_smallint
|
Chris@16
|
105 {
|
Chris@16
|
106 public:
|
Chris@16
|
107 typedef IntType input_type;
|
Chris@16
|
108 typedef IntType result_type;
|
Chris@16
|
109
|
Chris@16
|
110 class param_type
|
Chris@16
|
111 {
|
Chris@16
|
112 public:
|
Chris@16
|
113
|
Chris@16
|
114 typedef uniform_smallint distribution_type;
|
Chris@16
|
115
|
Chris@16
|
116 /** constructs the parameters of a @c uniform_smallint distribution. */
|
Chris@16
|
117 param_type(IntType min_arg = 0, IntType max_arg = 9)
|
Chris@16
|
118 : _min(min_arg), _max(max_arg)
|
Chris@16
|
119 {
|
Chris@16
|
120 BOOST_ASSERT(_min <= _max);
|
Chris@16
|
121 }
|
Chris@16
|
122
|
Chris@16
|
123 /** Returns the minimum value. */
|
Chris@16
|
124 IntType a() const { return _min; }
|
Chris@16
|
125 /** Returns the maximum value. */
|
Chris@16
|
126 IntType b() const { return _max; }
|
Chris@16
|
127
|
Chris@16
|
128
|
Chris@16
|
129 /** Writes the parameters to a @c std::ostream. */
|
Chris@16
|
130 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
|
Chris@16
|
131 {
|
Chris@16
|
132 os << parm._min << " " << parm._max;
|
Chris@16
|
133 return os;
|
Chris@16
|
134 }
|
Chris@16
|
135
|
Chris@16
|
136 /** Reads the parameters from a @c std::istream. */
|
Chris@16
|
137 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
|
Chris@16
|
138 {
|
Chris@16
|
139 is >> parm._min >> std::ws >> parm._max;
|
Chris@16
|
140 return is;
|
Chris@16
|
141 }
|
Chris@16
|
142
|
Chris@16
|
143 /** Returns true if the two sets of parameters are equal. */
|
Chris@16
|
144 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
|
Chris@16
|
145 { return lhs._min == rhs._min && lhs._max == rhs._max; }
|
Chris@16
|
146
|
Chris@16
|
147 /** Returns true if the two sets of parameters are different. */
|
Chris@16
|
148 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
|
Chris@16
|
149
|
Chris@16
|
150 private:
|
Chris@16
|
151 IntType _min;
|
Chris@16
|
152 IntType _max;
|
Chris@16
|
153 };
|
Chris@16
|
154
|
Chris@16
|
155 /**
|
Chris@16
|
156 * Constructs a @c uniform_smallint. @c min and @c max are the
|
Chris@16
|
157 * lower and upper bounds of the output range, respectively.
|
Chris@16
|
158 */
|
Chris@16
|
159 explicit uniform_smallint(IntType min_arg = 0, IntType max_arg = 9)
|
Chris@16
|
160 : _min(min_arg), _max(max_arg) {}
|
Chris@16
|
161
|
Chris@16
|
162 /**
|
Chris@16
|
163 * Constructs a @c uniform_smallint from its parameters.
|
Chris@16
|
164 */
|
Chris@16
|
165 explicit uniform_smallint(const param_type& parm)
|
Chris@16
|
166 : _min(parm.a()), _max(parm.b()) {}
|
Chris@16
|
167
|
Chris@16
|
168 /** Returns the minimum value of the distribution. */
|
Chris@16
|
169 result_type a() const { return _min; }
|
Chris@16
|
170 /** Returns the maximum value of the distribution. */
|
Chris@16
|
171 result_type b() const { return _max; }
|
Chris@16
|
172 /** Returns the minimum value of the distribution. */
|
Chris@16
|
173 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _min; }
|
Chris@16
|
174 /** Returns the maximum value of the distribution. */
|
Chris@16
|
175 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _max; }
|
Chris@16
|
176
|
Chris@16
|
177 /** Returns the parameters of the distribution. */
|
Chris@16
|
178 param_type param() const { return param_type(_min, _max); }
|
Chris@16
|
179 /** Sets the parameters of the distribution. */
|
Chris@16
|
180 void param(const param_type& parm)
|
Chris@16
|
181 {
|
Chris@16
|
182 _min = parm.a();
|
Chris@16
|
183 _max = parm.b();
|
Chris@16
|
184 }
|
Chris@16
|
185
|
Chris@16
|
186 /**
|
Chris@16
|
187 * Effects: Subsequent uses of the distribution do not depend
|
Chris@16
|
188 * on values produced by any engine prior to invoking reset.
|
Chris@16
|
189 */
|
Chris@16
|
190 void reset() { }
|
Chris@16
|
191
|
Chris@16
|
192 /** Returns a value uniformly distributed in the range [min(), max()]. */
|
Chris@16
|
193 template<class Engine>
|
Chris@16
|
194 result_type operator()(Engine& eng) const
|
Chris@16
|
195 {
|
Chris@16
|
196 typedef typename Engine::result_type base_result;
|
Chris@16
|
197 return generate(eng, boost::is_integral<base_result>());
|
Chris@16
|
198 }
|
Chris@16
|
199
|
Chris@16
|
200 /** Returns a value uniformly distributed in the range [param.a(), param.b()]. */
|
Chris@16
|
201 template<class Engine>
|
Chris@16
|
202 result_type operator()(Engine& eng, const param_type& parm) const
|
Chris@16
|
203 { return uniform_smallint(parm)(eng); }
|
Chris@16
|
204
|
Chris@16
|
205 /** Writes the distribution to a @c std::ostream. */
|
Chris@16
|
206 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, uniform_smallint, ud)
|
Chris@16
|
207 {
|
Chris@16
|
208 os << ud._min << " " << ud._max;
|
Chris@16
|
209 return os;
|
Chris@16
|
210 }
|
Chris@16
|
211
|
Chris@16
|
212 /** Reads the distribution from a @c std::istream. */
|
Chris@16
|
213 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, uniform_smallint, ud)
|
Chris@16
|
214 {
|
Chris@16
|
215 is >> ud._min >> std::ws >> ud._max;
|
Chris@16
|
216 return is;
|
Chris@16
|
217 }
|
Chris@16
|
218
|
Chris@16
|
219 /**
|
Chris@16
|
220 * Returns true if the two distributions will produce identical
|
Chris@16
|
221 * sequences of values given equal generators.
|
Chris@16
|
222 */
|
Chris@16
|
223 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(uniform_smallint, lhs, rhs)
|
Chris@16
|
224 { return lhs._min == rhs._min && lhs._max == rhs._max; }
|
Chris@16
|
225
|
Chris@16
|
226 /**
|
Chris@16
|
227 * Returns true if the two distributions may produce different
|
Chris@16
|
228 * sequences of values given equal generators.
|
Chris@16
|
229 */
|
Chris@16
|
230 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(uniform_smallint)
|
Chris@16
|
231
|
Chris@16
|
232 private:
|
Chris@16
|
233
|
Chris@16
|
234 // \cond show_private
|
Chris@16
|
235 template<class Engine>
|
Chris@16
|
236 result_type generate(Engine& eng, boost::mpl::true_) const
|
Chris@16
|
237 {
|
Chris@16
|
238 // equivalent to (eng() - eng.min()) % (_max - _min + 1) + _min,
|
Chris@16
|
239 // but guarantees no overflow.
|
Chris@16
|
240 typedef typename Engine::result_type base_result;
|
Chris@16
|
241 typedef typename boost::make_unsigned<base_result>::type base_unsigned;
|
Chris@16
|
242 typedef typename boost::make_unsigned<result_type>::type range_type;
|
Chris@16
|
243 range_type range = random::detail::subtract<result_type>()(_max, _min);
|
Chris@16
|
244 base_unsigned base_range =
|
Chris@16
|
245 random::detail::subtract<result_type>()((eng.max)(), (eng.min)());
|
Chris@16
|
246 base_unsigned val =
|
Chris@16
|
247 random::detail::subtract<base_result>()(eng(), (eng.min)());
|
Chris@16
|
248 if(range >= base_range) {
|
Chris@16
|
249 return boost::random::detail::add<range_type, result_type>()(
|
Chris@16
|
250 static_cast<range_type>(val), _min);
|
Chris@16
|
251 } else {
|
Chris@16
|
252 base_unsigned modulus = static_cast<base_unsigned>(range) + 1;
|
Chris@16
|
253 return boost::random::detail::add<range_type, result_type>()(
|
Chris@16
|
254 static_cast<range_type>(val % modulus), _min);
|
Chris@16
|
255 }
|
Chris@16
|
256 }
|
Chris@16
|
257
|
Chris@16
|
258 template<class Engine>
|
Chris@16
|
259 result_type generate(Engine& eng, boost::mpl::false_) const
|
Chris@16
|
260 {
|
Chris@16
|
261 typedef typename Engine::result_type base_result;
|
Chris@16
|
262 typedef typename boost::make_unsigned<result_type>::type range_type;
|
Chris@16
|
263 range_type range = random::detail::subtract<result_type>()(_max, _min);
|
Chris@16
|
264 base_result val = boost::uniform_01<base_result>()(eng);
|
Chris@16
|
265 // what is the worst that can possibly happen here?
|
Chris@16
|
266 // base_result may not be able to represent all the values in [0, range]
|
Chris@16
|
267 // exactly. If this happens, it will cause round off error and we
|
Chris@16
|
268 // won't be able to produce all the values in the range. We don't
|
Chris@16
|
269 // care about this because the user has already told us not to by
|
Chris@16
|
270 // using uniform_smallint. However, we do need to be careful
|
Chris@16
|
271 // to clamp the result, or floating point rounding can produce
|
Chris@16
|
272 // an out of range result.
|
Chris@16
|
273 range_type offset = static_cast<range_type>(val * (static_cast<base_result>(range) + 1));
|
Chris@16
|
274 if(offset > range) return _max;
|
Chris@16
|
275 return boost::random::detail::add<range_type, result_type>()(offset , _min);
|
Chris@16
|
276 }
|
Chris@16
|
277 // \endcond
|
Chris@16
|
278
|
Chris@16
|
279 result_type _min;
|
Chris@16
|
280 result_type _max;
|
Chris@16
|
281 };
|
Chris@16
|
282
|
Chris@16
|
283 } // namespace random
|
Chris@16
|
284
|
Chris@16
|
285 using random::uniform_smallint;
|
Chris@16
|
286
|
Chris@16
|
287 } // namespace boost
|
Chris@16
|
288
|
Chris@16
|
289 #endif // BOOST_RANDOM_UNIFORM_SMALLINT_HPP
|