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

Link :C++11 random number generator

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


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.


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 ;

Engine adaptor
4 template <class Engine, size_t p, size_t r >
class discard_block_engine ;
5 template < class Engine, size_t w, class UIntType >
class independent_bits_engine;
6 template< class Engine, size_t k >
class shuffle_order_engine;

Adaptors/engine with predefined parameters

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 >
6 typedef subtract_with_carry_engine< _ULonglong, 48, 5, 12 >
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;


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 templateclass uniform_int_distribution ;
2 template class uniform_real_distribution;
3 class bernoulli_distribution;
4 template class binomial_distribution;
5 template class geometric_distribution;
6 template class negative_binomial_distribution;
7 template class poisson_distribution;
8 template< class RealType = double >class exponential_distribution;
9 template class gamma_distribution;
10 template 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 chi_squared_distribution;
15 template class cauchy_distribution;
16 template class fisher_f_distribution;
17 template class student_t_distribution;
18 template class discrete_distribution;
19 template 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’.

#include &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;iostream&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;
#include &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;random&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;

using namespace std ;

int main( )
default_random_engine e ;

uniform_real_distribution&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt; double &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt; 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&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;5 ; i++)
cout&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt; e( ) &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt; ” ” ;

cout&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt; “\n\n” ;

//Outputting random values using engine and distribution
for(auto i=0 ; i&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;5 ; i++)
cout&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt; di(e) &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt; ” ” ;

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