RandomLib  1.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
Random integers
Back to The seed. Forward to Random real numbers. Up to Contents.

The following routines return random integers of the given type uniformly distributed in [min, max]. These are member functions of Random. Here IntType is any integer type, b, the bitcount, is an integer, w = Random::width. For the a 32-bit (resp. 64-bit) random generator, result_type is an unsigned int (resp. unsigned long long int) and w = 32 (resp. 64). The "C" in IntegerC stands for "closed".

Definitions of routines returning integers
routine type min max
Integer<IntType, b>() IntType 0 2b − 1
Integer<b>() unsigned 0 2b − 1
Integer(); unsigned 0 std::numeric_limits<unsigned>::max()
Integer<IntType>() IntType std::numeric_limits<IntType>::min() std::numeric_limits<IntType>::max()
Boolean() bool false true
Integer<IntType>(IntType n) IntType 0 n − 1
(if n = 0, std::numeric_limits<IntType>::max();
if n < 0, 0)
IntegerC<IntType>(IntType n) IntType 0 n
(if n < 0, 0)
IntegerC<IntType>(IntType m, IntType n) IntType m n
(if n < m, m)
operator()() Random::result_type 0 2w − 1
operator()(result_type n) Random::result_type 0 n − 1
(if n = 0, std::numeric_limits<result_type>::max())

Here are some examples of calling these routines

RandomLib::Random r; r.Reseed();
r(); // unsigned in [0, 2^32)
r(52); // unsigned in [0, 52)
r.Integer(); // unsigned in [0, 2^32)
r.Integer<int>(); // int in [-2^31, 2^31)
r.Integer<unsigned char>(); // unsigned char in [0, 256)
r.Integer<8>(); // unsigned in [0, 256)
r.Integer<int, 8>(); // int in [0, 256)
r.Boolean(); // boolean in [false, true]
r.Integer(52); // int in [0, 52)
r.Integer(52u); // unsigned in [0, 52)
r.IntegerC(51); // int in [0, 51]
r.IntegerC(1,52); // int in [1, 52]
Back to The seed. Forward to Random real numbers. Up to Contents.