C++11 difference between random engine and predefined engine

In C++11 random engine and predefined engine are almost the same but why are they call a predefined engine? here we will see the difference between random engine and predefined engine.

All the predefined engines parameters are fixed and they cannot be changed.Since every random-number generator engine algorithm are dependent on the parameters value to generate the random sequence,fixing the parameters will somehow restrict to some extend on how the engine can be implemented.

If the parameters are constant the state of the engine is the only changeable quantity,which in turn means it is the only source for the random sequence generation.Thus default constructed predefined engine failed to generate numerous different random sequence.

Link :Predefined engine list

For instance,lets take the ‘minstd_rand0‘ predefined engine of linear_congruential_engine.The linear_congruential_engine implement the algorithm ‘(a * xi + c) mod m ‘ to generate the random sequence.

In ‘minstd_rand0’ the parameter values of ‘a’ , ‘c’ and ‘m’ are fixed,so only the ‘xi‘ quantity,which is the state of the engine remains variant.

(Note ‘a’ , ‘c’ and ‘m’ are the second,third and fourth parameters,in ‘linear_congruential_engine’ declaration and the value of ‘a’ is 16807 , ‘c’ is 0 and ‘m’ is 2147483647;refer the minstd_rand0 engine).

This means the source of randomness is entirely dependent only on the ‘xi‘ quantity.In other words the default constructed ‘minstd_rand0’ object will always give the same random sequence in all the implementation.However in case of normal engine it can be manipulated by changing the parameters of ‘a’, ‘c’ and ‘m’ to give different random sequence for wider implementation.Examine the code example given below.

minstd_rand0 mr; //default constructor call mindts_rand0 object

for(int i=0 ; i< ; i++)
{
cout<< mr() << ” ” ;
}

cout<<“\n\n” ;
minstd_rand0 mr1; //default constructor call minstd_rand0 object

for(int i=0 ; i< ; i++)
{
cout<< mr1() << ” ” ;
}

/* minstd_rand0< unsigned , 120 , 0 , 1000> mr3 ; //error! cannot change parameters value */

Output in all the compiler,

16807
282475249
1622650073
984943658
 
16807
282475249
1622650073
984943658

The output is the same for the two minstd_rand0 objects and it will be in all the compiler.If you pass a seed however the random sequence generated is different but that is a different story.

Here is another code example using linear_congruential_engine.Note the different parameters value passed.

linear_congruential_engine<unsigned int , 16807 , 0 , 2147483 > lce1 ;

for(auto i=0 ; i<5 ; i++ )
{
cout<< lce1( ) << endl ;
}

cout<< “\n\n” ;
linear_congruential_engine<unsigned int , 1200 , 0 , 22222 > lce2 ;

for(auto i=0 ; i<5 ; i++ )
{
cout<&tlt; lce2( ) << endl ;
}

Output in Code::Blocks ,

16807
1154976
582795
365602
723951
 
1200
17792
17280
2874
4390

You can see that passing different parameters value to the same linear_congruential_engine generate different random sequence.If we keep on changing the parameters value the random sequence generated will also keep on changing.In this way we can obtain infinite different random sequence.