C++11 mersenne twister engine random number generator

The C++11 mersenne twister engine (or mersenne_twister_engine) generate unsigned integer random numbers in the closed interval [0 , 2w − 1].The declaration of the class is shown below.

Link :C++11 random number generator
 

template<class UIntType , size_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

All the types and member functions of the meresenne_twister_engine class is given below.


types and characteristics

typedef UIntType result_type; 

static constexpr size_t word_size = w;

static constexpr size_t state_size = n; 

static constexpr size_t shift_size = m; 

static constexpr size_t mask_bits = r; 

static constexpr UIntType xor_mask = a; 

static constexpr size_t tempering_u = u; 

static constexpr UIntType tempering_d = d; 

static constexpr size_t tempering_s = s; 

static constexpr UIntType tempering_b = b; 

static constexpr size_t tempering_t = t; 

static constexpr UIntType tempering_c = c; 

static constexpr size_t tempering_l = l; 

static constexpr UIntType initialization_multiplier = f; 

static constexpr result_type min() { return 0; } 

static constexpr result_type max() { return 2<sup>w</sup> − 1; } 

static constexpr result_type default_seed = 5489u; 

The following relation shall hold strictly,if any of the relation does not agree with the values passed as parameters you will get an error :

0 < m ,
m <= n ,
2u < w ,
r <= w ,
u <= w ,
s <= w ,
t <= w ,
l <= w ,
w <= numeric_limits<UIntType>::digits ,
a <= ( 1u<<w ) – 1u ,
b <= ( 1u<<w ) – 1u ,
c <= ( 1u<<w ) – 1u ,
d <= ( 1u<<w ) – 1u , and
f <= ( 1u<<w ) – 1u .

The function min() and max() will give the minimum and maximum random number the engine can generate.

 mersenne_twister_engine<unsigned int , 32, 624, 397, 31,
  0x9908b0dfUL, 11 ,
  0xffffffffUL, 7 ,
  0x9d2c5680UL, 15 ,
  0xefc60000UL, 18 , 1812433253UL > mte ;

cout<< mte.min() << endl
<< mte.max() ;

Output

0
4294967295



Constructors

C++11 mersenne_twister_engine has two constructors.

explicit mersenne_twister_engine(result_type value = default_seed); //Default constructor

template<class Sseq> explicit mersenne_twister_engine(Sseq& q); //Constructor accepting a seed

A code example is given below.

 mersenne_twister_engine<unsigned int , 32, 624, 397, 31,
  0x9908b0dfUL, 11 ,
  0xffffffffUL, 7 ,
  0x9d2c5680UL, 15 ,
  0xefc60000UL, 18 , 1812433253UL > mte , //default constructor called
mte1(89 ); //calls the overloaded constructor accepting a seed.
 

Generating function

The generating functions generate the random sequence.

result_type operator()( ); //Generate random sequence

void discard(unsigned long long z);//Engine state is jump to the state next to 'current state+z' state
operator()

To get the random sequence using the engine we will use the ‘operator()‘ function.

mersenne_twister_engine<unsigned int , 32, 624, 397, 31,
  0x9908b0dfUL, 11 ,
  0xffffffffUL, 7 ,
  0x9d2c5680UL, 15 ,
  0xefc60000UL, 18 , 1812433253UL > mte ;

for(auto i=0 ; i<5 ; i++ )
{
cout<< mte() << ” ” ; //Use the operator() function to generate the random sequence
}

Output in code::blocks,

3499211612   581869302   3890346734   3586334585   545404204

If you use the same parameters value you will get the same output in Visual Studio.



discard(unsigned long long z)

Every engine random number is associated with it’s state.The state determine which number is generated,if you can reproduce the same sate you will get the same random number.The ‘discard’ function help to jump the sate of the engine to the state after the ‘n’ state from the current state.Explaining the concept of ‘discard’ function verbally is rather arduous,so consider the code example.(Note every objects of the engine has the same initial state).

mersenne_twister_engine<unsigned int , 32, 624, 397, 31,
  0x9908b0dfUL, 11 ,
  0xffffffffUL, 7 ,
  0x9d2c5680UL, 15 ,
  0xefc60000UL, 18 , 1812433253UL > mte1 , mte2 ; ///mte1 and mte2 have the same initial state,so they give the same sequence

//gives the random sequence till the 6th state
for(auto i=0 ; i<6 ; i++ )
{
cout<< mte1() << ” ” ;
}

cout<< “\n\n” ;

cout<< mte2() << ” ” ; //first state engine output
cout<< mte2() << endl ; //second state engine output

mte2.discard( 2 ); ///jump the state of the engine to the 5th state

cout<< mte2() << ” ” ; //5th state engine output
cout<< mye2() << endl ; //6th state engine output 

Output in code::blocks,

3499211612   581869302   3890346734   3586334585   545404204   4161255391
 
3499211612   581869302 (output of 1st and 2nd state engine)
 
545404204   4161255391 (output of 5th and 6th state engine)

Since the state before calling the ‘discard’ is the 2nd state,after calling it two state is avoided which means ‘current state(2) +2=4’ ,the 3rd and 4th state is jumped and the 5th state is executed.


Seed generating functions

void seed(result_type s = default_seed);

template<class Sseq&gt; void seed(Sseq& q); //a templatized version of the above function

Link :What is seed in random number generator?

The seed function allows you to set the seed of the engine.Using this function you can either set the state to the initial state to obtain the same random sequence Or you can provide a new seed to interrupt the state of the engine and generate a wholly new sequence.

 mersenne_twister_engine<unsigned int , 32, 624, 397, 31,
  0x9908b0dfUL, 11 ,
  0xffffffffUL, 7 ,
  0x9d2c5680UL, 15 ,
  0xefc60000UL, 18 , 1812433253UL > mte;
for(auto i=0; i<4 ; i++)
{
cout<< mte( ) << ” ” ;
}

cout<< “\n\n” ;
mte.seed( ); //set the sate to the initial state

//generate same output as above
for(auto i=0 ; i<4 ; i++)
{
cout<< mte( ) << ” ” ;
}

Output in code::blocks,

3499211612   581869302   3890346734   3586334585

3499211612   581869302   3890346734   3586334585

If the state is not set to it’s initial state,the generated sequence will be different.