Monday, May 19, 2025

random number engines

Overview
Multiple classes are provided to generate pseudo random numbers using mathematical functions. Also a truly random device is also provided.

Details
These can be classified as below.
pseudo random number generator
These are template based generator. An uniform random number generator is a functor returning unsigned integer values such that each value in the range of possible results has (ideally) equal probability of being returned. These are pseudo random values as they are produced from a mathematical function.
NameDescription
linear_congruential_engine➹Linear congruential random number engine.
Definition
/*
UIntType:
An unsigned integer type. Values produced by the engine are of this type.
a:
The multiplier parameter (a) used in the transition algorithm. If m is not zero, this parameter should be lower than m.
c:
The increment parameter (c) used in the transition algorithm.If m is not zero, this parameter should be lower than m.
m:
The modulus parameter (m) used in the transition algorithm, except if this parameter is zero. If this parameter is zero, the value assumed for m on all operations is 
numerics_limits<UIntType>::max() + 1.
*/
template <class UIntTypeUIntType a, UIntType c, UIntType m>
class linear_congruential_engine
mersenne_twister_engine➹Mersenne twister random number engine.
Definition
/*
UIntType
An unsigned integer type. Values produced by the engine are of this type.
w
Word size: Number of bits of each word in the state sequence.
This parameter should be lower or equal to numeric_limits<UIntType>::digits.
n
State size: Number of elements in the state sequence. This determines the degree of recurrence in the generated series.
m
Shift size: On each twist, the elements are transformed using other values in the sequence that are m elements away.
This parameter should be lower than or equal to n.
r
Mask bits: The number of bits that mark the separation point of words on each twist.
This parameter should be lower than or equal to w.
a
Xor Mask: The XOR mask to be applied as the linear function on each twist.
This parameter should be lower than (1u<<w).
s, t, u, l
Tempering shift parameters: Shift values for the scrambling operation used by the generation algorithm.
These parameters should be lower than or equal to w.
b, c, d
Tempering bitmask parameters: Bitmask values for the scrambling operation used by the generation algorithm.
These parameters should be lower than (1u<<w).
f
Initialization multiplier: The initialization multiplier used to seed the state sequence when a single value is used as seed.
*/
template <class UIntTypesize_t w, size_t n, size_t m, size_t r,  UIntType a, size_t u, UIntType d, size_t s, UIntType b, size_t t, UIntType c, size_t l, UIntType f>  
class mersenne_twister_engine;
subtract_with_carry_engine➹Subtract-with-carry random number engine.
Definition
/*
UIntType
An unsigned integer type.
Values produced by the engine are of this type.
w
Word size: Number of bits of each word in the state sequence.
This parameter should be greater than zero and lower than numeric_limits<UIntType>::digits.
s
Short lag: Number of elements between advances.
This parameter should be greater than zero and lower than r.
r
Long lag: Distance between operand values. This determines the degree of recurrence in the generated series.
*/
template <class UIntTypesize_t w, size_t s, size_t r>
class subtract_with_carry_engine

pseudo random number engine adaptors
A random number engine is a uniform random number generator that can be instantiated.
The pseudo random number engine adaptors are template based  adapters which are random number engines that takes values produced by some other random number engine and applies an algorithm to those values in order to deliver a sequence of values with different randomness properties.
NameDescription
discard_block_engine➹Discard-block random number engine adaptor
Definition
/*
Engine
A random number engine type. All standard generators, except random_device, are random number engine types.
p
Block size: Number of elements in each block.
r
Used block: Number of elements in the block that are used (not discarded). The rest (p-r) are discarded.
This parameter should be greater than zero and lower than or equal to p.
*/
template <class Enginesize_t p, size_t r>
class discard_block_engine
independent_bits_engine➹Independent-bits random number engine adaptor
Definition
/*
Engine
A random number engine type. All standard generators, except random_device, are random number engine types.
w
Word size: Number of bits of each generated number.
UIntType
An unsigned integer type.
Values produced by the engine are of this type.
*/
template <class Enginesize_t w, class UIntType>
class independent_bits_engine
shuffle_order_engine➹Shuffle-order random number engine adaptor
Definition
/*
Engine
A random number engine type. All standard generators, except random_device, are random number engine types.
k
Table size: Number of elements in the buffer table.
This parameter should be greater than zero.
*/
template <class Enginesize_t k> 
class shuffle_order_engine

pseudo random number engine instances
These are instances defined in the std. library. These are based on  pseudo random number engines and adapters.
NameDescriptiontypedef
default_random_engine➹Default random engineImplementation specific
Example
    default_random_engine e(20180921);
    //prints 2025806668
    cout << e() << endl;
minstd_rand➹Minimal Standard minstd_randlinear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
Example
    minstd_rand  e(20180921);
    //prints 1343145500
    cout << e() << endl;
minstd_rand0➹Minimal Standard minstd_rand0linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
Example
    minstd_rand0  e(20180921);
    //prints 2025806668
    cout << e() << endl;
mt19937➹Mersenne Twister 19937mersenne_twister_engine<uint_fast32_t, 32,624,397,31,0x9908b0df,11,0xffffffff,7,0x9d2c5680,15,0xefc60000,18,1812433253>
Example
    mt19937  e(20180921);
    //prints 1204448992
    cout << e() << endl;
mt19937_64➹Mersenne Twister 19937mersenne_twister_engine<uint_fast64_t, 64,312,156,31,0xb5026f5aa96619e9, 29,0x5555555555555555, 17,0x71d67fffeda60000, 37,0xfff7eee000000000, 43,6364136223846793005>
Example
    mt19937_64 e(20180921);
    //prints 6843694179489687735
    cout << e() << endl;
ranlux24_base➹Ranlux 24 base generatorsubtract_with_carry_engine <uint_fast64_t, 48, 5, 12>
Example
    ranlux24_base e(20180921);
    //prints 2203727
    cout << e() << endl;
ranlux48_base➹Ranlux 48 basesubtract_with_carry_engine <uint_fast64_t, 48, 5, 12>
Example
    ranlux48_base e(20180921);
    //prints 270782120960079
    cout << e() << endl;
ranlux24➹Ranlux 24 discard_block_engine <ranlux24_base, 223, 23>
Example
    ranlux24 e(20180921);
    //prints 2203727
    cout << e() << endl;
ranlux48➹Ranlux 48 discard_block_engine <ranlux48_base, 389, 11>
Example
    ranlux48 e(20180921);
    //prints 270782120960079
    cout << e() << endl;
knuth_b➹Knuth-B shuffle_order_engine <minstd_rand0,256>
Example
    knuth_b e(20180921);
    //prints 1438635823
    cout << e() << endl;

The following diagram shows the relations.

random value generator
This is a Non-deterministic random number generator.
NameDescription
random_device➹True random number generator.
Example
    random_device  e;
    //prints 818932322
    cout << e() << endl;

interface
All engines provide same interface. Constructor might differ for random_device based on implementation. The following describes the interface for default_random_engine➹.


No comments:

Post a Comment