uniform_smallint.hpp 11 KB

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