uniform_smallint.hpp 12 KB

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