C++11 random mersenne_twister_engine


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

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

Please visit the link Mersenne Twister- wikipedia to read about the algorithm this engine employ to generate the random numbers.

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 2w − 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.

Code example

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


 


Stop wasting time,earn money($$$) from your website-Join Now!

Constructors

explicit mersenne_twister_engine(result_type value = default_seed);  //Default constructor
template explicit mersenne_twister_engine(Sseq& q); //Constructor accepting a seed

Code example

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

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.

Code example

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).

Code example

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> void seed(Sseq& q); //a templatized version of the above function

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.

Code example

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.