The <random> header file of C++(introduced in C++11) provides C++ programmer with a random number generator mechanism rendering random numbers for various purposes and needs.Like other C++ standard libraries the random number generator library consist of STL template classes and functions to meet the requirement of various possible needs.As mentioned 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> as 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;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 it simply refer to 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 the 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

1 template< class UIntType, UIntType a, UIntType c, UIntType m >
class linear_congruential_engine;
2 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;
3 template < class UIntType, size_t w, size_t s, size_t r >
class subtract_with_carry_engine ;

4 template <class Engine, size_t p, size_t r >
5 template < class Engine, size_t w, class UIntType >
class independent_bits_engine;
6 template< class Engine, size_t k >
class shuffle_order_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;

Visit the link C++11 random engine and engine adapters with predefined parameters to know more about the predefined engine.

#### Utilities

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

1 class random_device;
2 class seed_seq;
3 template< class RealType, size_t bits, class URNG >
RealType generate_canonical(URNG& g);

#### Distribution list

1 template<class IntType = int >
class uniform_int_distribution ;
2 template <class RealType = double >
class uniform_real_distribution;
3 class bernoulli_distribution;
4 template <class IntType = int >
class binomial_distribution;
5 template <class IntType = int >
class geometric_distribution;
6 template <class IntType = int >
class negative_binomial_distribution;
7 template <class IntType = int >
class poisson_distribution;
8 template< class RealType = double >
class exponential_distribution;
9 template <class RealType = double >
class gamma_distribution;
10 template <class RealType = double >
class weibull_distribution;
11 template< class RealType = double >
class extreme_value_distribution;
12 template< class RealType = double >
class normal_distribution;
13 template< class RealType = double >
class lognormal_distribution;
14 template <class RealType = double >
class chi_squared_distribution;
15 template <class RealType = double >
class cauchy_distribution;
16 template <class RealType = double >
class fisher_f_distribution;
17 template <class RealType = double >
class student_t_distribution;
18 template <class IntType = int >
class discrete_distribution;
19 template <class RealType = double >
class piecewise_constant_distribution ;
20 template< class RealType = double >
class piecewise_linear_distribution;

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

Code example

#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