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