1 /* boost random/uniform_int.hpp header file
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)
8 * See http://www.boost.org for most recent version including documentation.
13 * 2001-04-08 added min<max assertion (N. Becker)
14 * 2001-02-18 moved to individual header files
17 #ifndef BOOST_RANDOM_UNIFORM_INT_HPP
18 #define BOOST_RANDOM_UNIFORM_INT_HPP
22 #include <boost/config.hpp>
23 #include <boost/limits.hpp>
24 #include <boost/static_assert.hpp>
25 #include <boost/detail/workaround.hpp>
26 #include <boost/random/uniform_smallint.hpp>
27 #include <boost/random/detail/config.hpp>
28 #include <boost/random/detail/signed_unsigned_tools.hpp>
29 #include <boost/type_traits/make_unsigned.hpp>
30 #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
31 #include <boost/type_traits/is_float.hpp>
36 // uniform integer distribution on [min, max]
37 template<class IntType
= int>
41 typedef IntType input_type
;
42 typedef IntType result_type
;
43 typedef typename make_unsigned
<result_type
>::type range_type
;
45 explicit uniform_int(IntType min_arg
= 0, IntType max_arg
= 9)
46 : _min(min_arg
), _max(max_arg
)
48 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
49 // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
50 BOOST_STATIC_ASSERT(std::numeric_limits
<IntType
>::is_integer
);
52 assert(min_arg
<= max_arg
);
56 result_type min
BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _min
; }
57 result_type max
BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _max
; }
60 // can't have member function templates out-of-line due to MSVC bugs
61 template<class Engine
>
62 result_type
operator()(Engine
& eng
)
64 return generate(eng
, _min
, _max
, _range
);
67 template<class Engine
>
68 result_type
operator()(Engine
& eng
, result_type n
)
77 return generate(eng
, 0, n
- 1, n
- 1);
80 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
81 template<class CharT
, class Traits
>
82 friend std::basic_ostream
<CharT
,Traits
>&
83 operator<<(std::basic_ostream
<CharT
,Traits
>& os
, const uniform_int
& ud
)
85 os
<< ud
._min
<< " " << ud
._max
;
89 template<class CharT
, class Traits
>
90 friend std::basic_istream
<CharT
,Traits
>&
91 operator>>(std::basic_istream
<CharT
,Traits
>& is
, uniform_int
& ud
)
93 is
>> std::ws
>> ud
._min
>> std::ws
>> ud
._max
;
100 template<class Engine
>
101 static result_type
generate(Engine
& eng
, result_type min_value
, result_type max_value
, range_type range
)
103 typedef typename
Engine::result_type base_result
;
104 // ranges are always unsigned
105 typedef typename make_unsigned
<base_result
>::type base_unsigned
;
106 const base_result bmin
= (eng
.min
)();
107 const base_unsigned brange
=
108 random::detail::subtract
<base_result
>()((eng
.max
)(), (eng
.min
)());
112 } else if(brange
== range
) {
113 // this will probably never happen in real life
114 // basically nothing to do; just take care we don't overflow / underflow
115 base_unsigned v
= random::detail::subtract
<base_result
>()(eng(), bmin
);
116 return random::detail::add
<base_unsigned
, result_type
>()(v
, min_value
);
117 } else if(brange
< range
) {
118 // use rejection method to handle things like 0..3 --> 0..4
120 // concatenate several invocations of the base RNG
121 // take extra care to avoid overflows
123 if(range
== (std::numeric_limits
<range_type
>::max
)()) {
124 limit
= range
/(range_type(brange
)+1);
125 if(range
% range_type(brange
)+1 == range_type(brange
))
128 limit
= (range
+1)/(range_type(brange
)+1);
130 // We consider "result" as expressed to base (brange+1):
131 // For every power of (brange+1), we determine a random factor
132 range_type result
= range_type(0);
133 range_type mult
= range_type(1);
134 while(mult
<= limit
) {
135 result
+= random::detail::subtract
<base_result
>()(eng(), bmin
) * mult
;
136 mult
*= range_type(brange
)+range_type(1);
139 // range+1 is an integer power of brange+1: no rejections required
141 // range/mult < brange+1 -> no endless loop
142 result
+= uniform_int
<range_type
>(0, range
/mult
)(eng
) * mult
;
144 return random::detail::add
<range_type
, result_type
>()(result
, min_value
);
146 } else { // brange > range
147 if(brange
/ range
> 4 /* quantization_cutoff */ ) {
148 // the new range is vastly smaller than the source range,
149 // so quantization effects are not relevant
150 return boost::uniform_smallint
<result_type
>(min_value
, max_value
)(eng
);
152 // use rejection method to handle cases like 0..5 -> 0..4
154 base_unsigned result
=
155 random::detail::subtract
<base_result
>()(eng(), bmin
);
156 // result and range are non-negative, and result is possibly larger
157 // than range, so the cast is safe
158 if(result
<= static_cast<base_unsigned
>(range
))
159 return random::detail::add
<base_unsigned
, result_type
>()(result
, min_value
);
167 _range
= random::detail::subtract
<result_type
>()(_max
, _min
);
170 // The result_type may be signed or unsigned, but the _range is always
172 result_type _min
, _max
;
178 #endif // BOOST_RANDOM_UNIFORM_INT_HPP