Chris@16: /* boost random/uniform_smallint.hpp header file Chris@16: * Chris@16: * Copyright Jens Maurer 2000-2001 Chris@16: * Distributed under the Boost Software License, Version 1.0. (See Chris@16: * accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@16: * See http://www.boost.org for most recent version including documentation. Chris@16: * Chris@101: * $Id$ Chris@16: * Chris@16: * Revision history Chris@16: * 2001-04-08 added min Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace random { Chris@16: Chris@16: // uniform integer distribution on a small range [min, max] Chris@16: Chris@16: /** Chris@16: * The distribution function uniform_smallint models a \random_distribution. Chris@16: * On each invocation, it returns a random integer value uniformly distributed Chris@16: * in the set of integer numbers {min, min+1, min+2, ..., max}. It assumes Chris@16: * that the desired range (max-min+1) is small compared to the range of the Chris@16: * underlying source of random numbers and thus makes no attempt to limit Chris@16: * quantization errors. Chris@16: * Chris@16: * Let \f$r_{\mathtt{out}} = (\mbox{max}-\mbox{min}+1)\f$ the desired range of Chris@16: * integer numbers, and Chris@16: * let \f$r_{\mathtt{base}}\f$ be the range of the underlying source of random Chris@16: * numbers. Then, for the uniform distribution, the theoretical probability Chris@16: * for any number i in the range \f$r_{\mathtt{out}}\f$ will be Chris@16: * \f$\displaystyle p_{\mathtt{out}}(i) = \frac{1}{r_{\mathtt{out}}}\f$. Chris@16: * Likewise, assume a uniform distribution on \f$r_{\mathtt{base}}\f$ for Chris@16: * the underlying source of random numbers, i.e. Chris@16: * \f$\displaystyle p_{\mathtt{base}}(i) = \frac{1}{r_{\mathtt{base}}}\f$. Chris@16: * Let \f$p_{\mathtt{out\_s}}(i)\f$ denote the random Chris@16: * distribution generated by @c uniform_smallint. Then the sum over all Chris@16: * i in \f$r_{\mathtt{out}}\f$ of Chris@16: * \f$\displaystyle Chris@16: * \left(\frac{p_{\mathtt{out\_s}}(i)}{p_{\mathtt{out}}(i)} - 1\right)^2\f$ Chris@16: * shall not exceed Chris@16: * \f$\displaystyle \frac{r_{\mathtt{out}}}{r_{\mathtt{base}}^2} Chris@16: * (r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}}) Chris@16: * (r_{\mathtt{out}} - r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}})\f$. Chris@16: * Chris@16: * The template parameter IntType shall denote an integer-like value type. Chris@16: * Chris@16: * @xmlnote Chris@16: * The property above is the square sum of the relative differences Chris@16: * in probabilities between the desired uniform distribution Chris@16: * \f$p_{\mathtt{out}}(i)\f$ and the generated distribution Chris@16: * \f$p_{\mathtt{out\_s}}(i)\f$. Chris@16: * The property can be fulfilled with the calculation Chris@16: * \f$(\mbox{base\_rng} \mbox{ mod } r_{\mathtt{out}})\f$, as follows: Chris@16: * Let \f$r = r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}}\f$. Chris@16: * The base distribution on \f$r_{\mathtt{base}}\f$ is folded onto the Chris@16: * range \f$r_{\mathtt{out}}\f$. The numbers i < r have assigned Chris@16: * \f$\displaystyle Chris@16: * \left\lfloor\frac{r_{\mathtt{base}}}{r_{\mathtt{out}}}\right\rfloor+1\f$ Chris@16: * numbers of the base distribution, the rest has only \f$\displaystyle Chris@16: * \left\lfloor\frac{r_{\mathtt{base}}}{r_{\mathtt{out}}}\right\rfloor\f$. Chris@16: * Therefore, Chris@16: * \f$\displaystyle p_{\mathtt{out\_s}}(i) = Chris@16: * \left(\left\lfloor\frac{r_{\mathtt{base}}} Chris@16: * {r_{\mathtt{out}}}\right\rfloor+1\right) / Chris@16: * r_{\mathtt{base}}\f$ for i < r and \f$\displaystyle p_{\mathtt{out\_s}}(i) = Chris@16: * \left\lfloor\frac{r_{\mathtt{base}}} Chris@16: * {r_{\mathtt{out}}}\right\rfloor/r_{\mathtt{base}}\f$ otherwise. Chris@16: * Substituting this in the Chris@16: * above sum formula leads to the desired result. Chris@16: * @endxmlnote Chris@16: * Chris@16: * Note: The upper bound for Chris@16: * \f$(r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}}) Chris@16: * (r_{\mathtt{out}} - r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}})\f$ is Chris@16: * \f$\displaystyle \frac{r_{\mathtt{out}}^2}{4}\f$. Regarding the upper bound Chris@16: * for the square sum of the relative quantization error of Chris@16: * \f$\displaystyle \frac{r_\mathtt{out}^3}{4r_{\mathtt{base}}^2}\f$, it Chris@16: * seems wise to either choose \f$r_{\mathtt{base}}\f$ so that Chris@16: * \f$r_{\mathtt{base}} > 10r_{\mathtt{out}}^2\f$ or ensure that Chris@16: * \f$r_{\mathtt{base}}\f$ is Chris@16: * divisible by \f$r_{\mathtt{out}}\f$. Chris@16: */ Chris@16: template Chris@16: class uniform_smallint Chris@16: { Chris@16: public: Chris@16: typedef IntType input_type; Chris@16: typedef IntType result_type; Chris@16: Chris@16: class param_type Chris@16: { Chris@16: public: Chris@16: Chris@16: typedef uniform_smallint distribution_type; Chris@16: Chris@16: /** constructs the parameters of a @c uniform_smallint distribution. */ Chris@16: param_type(IntType min_arg = 0, IntType max_arg = 9) Chris@16: : _min(min_arg), _max(max_arg) Chris@16: { Chris@16: BOOST_ASSERT(_min <= _max); Chris@16: } Chris@16: Chris@16: /** Returns the minimum value. */ Chris@16: IntType a() const { return _min; } Chris@16: /** Returns the maximum value. */ Chris@16: IntType b() const { return _max; } Chris@16: Chris@16: Chris@16: /** Writes the parameters to a @c std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm) Chris@16: { Chris@16: os << parm._min << " " << parm._max; Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads the parameters from a @c std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm) Chris@16: { Chris@16: is >> parm._min >> std::ws >> parm._max; Chris@16: return is; Chris@16: } Chris@16: Chris@16: /** Returns true if the two sets of parameters are equal. */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs) Chris@16: { return lhs._min == rhs._min && lhs._max == rhs._max; } Chris@16: Chris@16: /** Returns true if the two sets of parameters are different. */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type) Chris@16: Chris@16: private: Chris@16: IntType _min; Chris@16: IntType _max; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * Constructs a @c uniform_smallint. @c min and @c max are the Chris@16: * lower and upper bounds of the output range, respectively. Chris@16: */ Chris@16: explicit uniform_smallint(IntType min_arg = 0, IntType max_arg = 9) Chris@16: : _min(min_arg), _max(max_arg) {} Chris@16: Chris@16: /** Chris@16: * Constructs a @c uniform_smallint from its parameters. Chris@16: */ Chris@16: explicit uniform_smallint(const param_type& parm) Chris@16: : _min(parm.a()), _max(parm.b()) {} Chris@16: Chris@16: /** Returns the minimum value of the distribution. */ Chris@16: result_type a() const { return _min; } Chris@16: /** Returns the maximum value of the distribution. */ Chris@16: result_type b() const { return _max; } Chris@16: /** Returns the minimum value of the distribution. */ Chris@16: result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _min; } Chris@16: /** Returns the maximum value of the distribution. */ Chris@16: result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _max; } Chris@16: Chris@16: /** Returns the parameters of the distribution. */ Chris@16: param_type param() const { return param_type(_min, _max); } Chris@16: /** Sets the parameters of the distribution. */ Chris@16: void param(const param_type& parm) Chris@16: { Chris@16: _min = parm.a(); Chris@16: _max = parm.b(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Effects: Subsequent uses of the distribution do not depend Chris@16: * on values produced by any engine prior to invoking reset. Chris@16: */ Chris@16: void reset() { } Chris@16: Chris@16: /** Returns a value uniformly distributed in the range [min(), max()]. */ Chris@16: template Chris@16: result_type operator()(Engine& eng) const Chris@16: { Chris@16: typedef typename Engine::result_type base_result; Chris@16: return generate(eng, boost::is_integral()); Chris@16: } Chris@16: Chris@16: /** Returns a value uniformly distributed in the range [param.a(), param.b()]. */ Chris@16: template Chris@16: result_type operator()(Engine& eng, const param_type& parm) const Chris@16: { return uniform_smallint(parm)(eng); } Chris@16: Chris@16: /** Writes the distribution to a @c std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, uniform_smallint, ud) Chris@16: { Chris@16: os << ud._min << " " << ud._max; Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads the distribution from a @c std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, uniform_smallint, ud) Chris@16: { Chris@16: is >> ud._min >> std::ws >> ud._max; Chris@16: return is; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns true if the two distributions will produce identical Chris@16: * sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(uniform_smallint, lhs, rhs) Chris@16: { return lhs._min == rhs._min && lhs._max == rhs._max; } Chris@16: Chris@16: /** Chris@16: * Returns true if the two distributions may produce different Chris@16: * sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(uniform_smallint) Chris@16: Chris@16: private: Chris@16: Chris@16: // \cond show_private Chris@16: template Chris@16: result_type generate(Engine& eng, boost::mpl::true_) const Chris@16: { Chris@16: // equivalent to (eng() - eng.min()) % (_max - _min + 1) + _min, Chris@16: // but guarantees no overflow. Chris@16: typedef typename Engine::result_type base_result; Chris@16: typedef typename boost::make_unsigned::type base_unsigned; Chris@16: typedef typename boost::make_unsigned::type range_type; Chris@16: range_type range = random::detail::subtract()(_max, _min); Chris@16: base_unsigned base_range = Chris@16: random::detail::subtract()((eng.max)(), (eng.min)()); Chris@16: base_unsigned val = Chris@16: random::detail::subtract()(eng(), (eng.min)()); Chris@16: if(range >= base_range) { Chris@16: return boost::random::detail::add()( Chris@16: static_cast(val), _min); Chris@16: } else { Chris@16: base_unsigned modulus = static_cast(range) + 1; Chris@16: return boost::random::detail::add()( Chris@16: static_cast(val % modulus), _min); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: result_type generate(Engine& eng, boost::mpl::false_) const Chris@16: { Chris@16: typedef typename Engine::result_type base_result; Chris@16: typedef typename boost::make_unsigned::type range_type; Chris@16: range_type range = random::detail::subtract()(_max, _min); Chris@16: base_result val = boost::uniform_01()(eng); Chris@16: // what is the worst that can possibly happen here? Chris@16: // base_result may not be able to represent all the values in [0, range] Chris@16: // exactly. If this happens, it will cause round off error and we Chris@16: // won't be able to produce all the values in the range. We don't Chris@16: // care about this because the user has already told us not to by Chris@16: // using uniform_smallint. However, we do need to be careful Chris@16: // to clamp the result, or floating point rounding can produce Chris@16: // an out of range result. Chris@16: range_type offset = static_cast(val * (static_cast(range) + 1)); Chris@16: if(offset > range) return _max; Chris@16: return boost::random::detail::add()(offset , _min); Chris@16: } Chris@16: // \endcond Chris@16: Chris@16: result_type _min; Chris@16: result_type _max; Chris@16: }; Chris@16: Chris@16: } // namespace random Chris@16: Chris@16: using random::uniform_smallint; Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_RANDOM_UNIFORM_SMALLINT_HPP