# C++ random header file C+11

The C++ <random> header file supply us with many elements that can help generate random numbers.The C++ <random> header file consist of STL template classes and functions which are part of Random number generator Engine,distribution,predefined engine,seed class,etc.

The library was introduced in C++11,it is also better than the C counterpart random number generator function ‘rand’ found in the library <ctsdlib> ;it generate a pseudo-random number sequence.

#### Engine and distribution

To understand the workings of the random number generator,you must take into account two of the most important aspects that make up the random number generator:

i)Engine and

ii)Distribution.

Here we see will various engines and distributions that are available in the <random> header file.Although we will not be going into the minute detail on the workings of engine and distribution,however a brief introduction will be provided here.Another post is dedicated entirely on the discussion of engine and distribution in general (link above);that post is quite a lengthy one I am sure you will have your heart content there :).

###### Engine

When we talk about ‘random number generator engine‘ or just ‘engine‘ ,we simply mean the process through which the random number is generated.Another way of interpreting ‘engine’ is to simply refer to it as the source of randomness.We can rightfully claim that if random generator engine is absent there could be no random number.

While discussing random number generator engine we will often come across the two terms:transition algorithm and generation algorithm,what they stand for is given below:

Transition algorithm:When we say transition algorithm we refer to the process utilize by the engine to move onto the next state of the engine.Note transition algorithm is usually a mathematical equation and so the engine’s state is some real values obtained from that equation.

Generation algorithm:The generation algorithm is the algorithm applied to obtained the random number.Note generation algorithm certainly utilize the engine’s state to produce the random number,this also means every random number generated is dependent on the engine’s state.So each random number produced is associated with different state of the engine.

###### Distribution

The role of distribution in random number generator is to specify how the random number generated by the engine is used.To simplify the understanding of ‘distribution’ one thing you must clearly digest is the engine itself will generate random number.But the engine itself mostly provides an integer random number,so what if you want a float random number or a double or bool type,the engine itself cannot fulfill this needs.

Here distribution come into play,by specifying the type of random number we want in distribution we can generate random numbers of the type;distribution act as a sidekick like Alfred or Nightwing to Batman(the engine).Another useful property of distribution is to impose certain restriction on the range of values within which the random number should be generated.

The list provided below are the engines,predefined engines and distribution available in C++11 <random> header.You can use any of the engine and distribution according to your needs.

***Note the default_random_engine is an implementation-specific type engine.It is the only engine that does not guarantee generation of identical value sequences on different machine and platforms.

(Visit each of the link to read about the discussion of the specific engine , distribution and predefined engine)

#### Engine list

The engines shown below are the engines and engine adaptor available in C++ <random> header file.

###### Basic engine

The list shown below are the engine available in C++ <random> header file with predefined parameters.

1 typedef linear_congruential_engine <unsigned int, 16807, 0, 2147483647 >
minstd_rand0 ;
2 typedef linear_congruential_engine<unsigned int, 48271, 0, 2147483647 >
minstd_rand ;
3 typedef mersenne_twister_engine< unsigned int , 32 , 624 , 397 , 31 , 0x9908b0df , 11 , 0xffffffff , 7 , 0x9d2c5680 , 15 , 0xefc60000 , 18 , 1812433253 >
mt19937 ;
4 typedef mersenne_twister_engine< _ULonglong, 64, 312, 156, 31,
0xb5026f5aa96619e9ULL, 29 ,
0x5555555555555555ULL, 17,
0x71d67fffeda60000ULL, 37,
0xfff7eee000000000ULL, 43,
6364136223846793005ULL > mt19937_64 ;
5 typedef subtract_with_carry_engine< unsigned int, 24, 10, 24 >
ranlux24_base;
6 typedef subtract_with_carry_engine< _ULonglong, 48, 5, 12 >
ranlux48_base;
7 typedef discard_block_engine< ranlux24_base, 223, 23 > ranlux24;
8 typedef discard_block_engine< ranlux48_base, 389, 11 > ranlux48;
9 typedef shuffle_order_engine< minstd_rand0, 256 > knuth_b;
10 typedef implementation-defined default_random_engine;

#### Utilities

Some supporting utilities to the random number generator and the random distribution.

#### Distribution list

A small code example is provided which generate random numbers utilizing ‘default_random_engine’ and ‘uniform_real_distribution’.

```#include <iostream>
#include <random>

using namespace std ;

int main( )
{
default_random_engine e ;

uniform_real_distribution< double > di( 2.310 , 2.89 ); /*specifying to generate ‘double’ type values between 2.310 and 2.89 */

//Outputting random values using only engine
for(auto i=0 ; i<5 ; i++)
{
cout<< e( ) << ” ” ;
}

cout<< “\n\n” ;

//Outputting random values using engine and distribution
for(auto i=0 ; i<5 ; i++)
{
cout<< di(e) << ” ” ;
}

cin.get( );
return 0;
}
```

Output in Code::block,(the output may vary in other compiler)

16807   282475249   1622650073   984943658   1144108930

2.33729   2.70399   2.53243   2.79196   2.34101   2.69927   2.53238   2.55214 2.65161   2.80078