12 #if !defined(RANDOMLIB_UNIFORMINTEGER_HPP)
13 #define RANDOMLIB_UNIFORMINTEGER_HPP 1
94 template<
class Random>
99 IntType
Min()
const {
return _a; }
103 IntType
Max()
const {
return _a + (IntType(1) << (_l * bits)) - 1; }
116 {
while (_l) Refine(r);
return _a; }
128 void Add(IntType c) { _a += c; }
141 if ( (q *
Max() < p))
return true;
142 if (!(q *
Min() < p))
return false;
155 template<
class Random>
167 template<
class Random>
179 template<
class Random>
192 static bool Check(IntType mmax, IntType qmax) {
193 return ( mmax - 1 <= ((std::numeric_limits<IntType>::max)() >> bits) &&
194 mmax - 1 <= (std::numeric_limits<IntType>::max)() / qmax );
198 template<
class Random>
static unsigned RandomDigit(
Random& r)
throw()
199 {
return unsigned(r.template Integer<bits>()); }
200 template<
class Random>
void Refine(
Random& r)
201 { _a += IntType(RandomDigit(r) << (bits * --_l)); }
204 template<
typename IntType,
int bits>
template<
class Random>
208 "UniformInteger: invalid integer type IntType");
210 "UniformInteger: IntType must be a signed type");
211 STATIC_ASSERT(bits > 0 && bits < std::numeric_limits<IntType>::digits &&
212 bits <= std::numeric_limits<unsigned>::digits,
213 "UniformInteger: bits out of range");
215 for (IntType v = 1, c = 0;;) {
217 for (IntType w = v, a = c, d = 1;;) {
223 IntType j = (a / m) * m; a -= j; w -= j;
225 if (a + d <= m) { _a = !flip ? a : m - a - d;
return; }
229 w <<= bits; a <<= bits; d <<= bits; ++_l;
231 IntType j = (v / m) * m; v -= j; c -= j;
232 v <<= bits; c <<= bits; c += IntType(RandomDigit(r));
241 template<
typename IntType,
int bits>
245 os <<
"[" << u.
Min() <<
"," << u.
Max() <<
"]";
253 #endif // RANDOMLIB_UNIFORMINTEGER_HPP
Generate random integers, reals, and booleans.
#define STATIC_ASSERT(cond, reason)
RandomCanonical< RandomGenerator > Random