+
+ // \cond show_private
+ template<class Engine>
+ result_type generate(Engine& eng, boost::mpl::true_) const
+ {
+ // equivalent to (eng() - eng.min()) % (_max - _min + 1) + _min,
+ // but guarantees no overflow.
+ typedef typename Engine::result_type base_result;
+ typedef typename boost::make_unsigned<base_result>::type base_unsigned;
+ typedef typename boost::make_unsigned<result_type>::type range_type;
+ range_type range = random::detail::subtract<result_type>()(_max, _min);
+ base_unsigned base_range =
+ random::detail::subtract<result_type>()((eng.max)(), (eng.min)());
+ base_unsigned val =
+ random::detail::subtract<base_result>()(eng(), (eng.min)());
+ if(range >= base_range) {
+ return boost::random::detail::add<range_type, result_type>()(
+ static_cast<range_type>(val), _min);
+ } else {
+ base_unsigned modulus = static_cast<base_unsigned>(range) + 1;
+ return boost::random::detail::add<range_type, result_type>()(
+ static_cast<range_type>(val % modulus), _min);
+ }
+ }
+
+ template<class Engine>
+ result_type generate(Engine& eng, boost::mpl::false_) const
+ {
+ typedef typename Engine::result_type base_result;
+ typedef typename boost::make_unsigned<result_type>::type range_type;
+ range_type range = random::detail::subtract<result_type>()(_max, _min);
+ base_result val = boost::uniform_01<base_result>()(eng);
+ // what is the worst that can possibly happen here?
+ // base_result may not be able to represent all the values in [0, range]
+ // exactly. If this happens, it will cause round off error and we
+ // won't be able to produce all the values in the range. We don't
+ // care about this because the user has already told us not to by
+ // using uniform_smallint. However, we do need to be careful
+ // to clamp the result, or floating point rounding can produce
+ // an out of range result.
+ range_type offset = static_cast<range_type>(val * (static_cast<base_result>(range) + 1));
+ if(offset > range) return _max;
+ return boost::random::detail::add<range_type, result_type>()(offset , _min);
+ }
+ // \endcond