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.
Link : C++ cstdlib rand function
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:
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 :).
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.
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)
The engines shown below are the engines and engine adaptor available in C++ <random> header file.
|4 template <class Engine, size_t p, size_t r >|
class discard_block_engine ;
|5 template < class Engine, size_t w, class UIntType >|
|6 template< class Engine, size_t k >|
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 >|
|2 typedef linear_congruential_engine<unsigned int, 48271, 0, 2147483647 >|
|3 typedef mersenne_twister_engine< unsigned int , 32 , 624 , 397 , 31 , 0x9908b0df , 11 , 0xffffffff , 7 , 0x9d2c5680 , 15 , 0xefc60000 , 18 , 1812433253 >|
|4 typedef mersenne_twister_engine< _ULonglong, 64, 312, 156, 31,|
0xb5026f5aa96619e9ULL, 29 ,
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;|
Visit the link C++11 random engine and engine adapters with predefined parameters to know more about the predefined 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);
A small code example is provided which generate random numbers utilizing ‘default_random_engine’ and ‘uniform_real_distribution’.
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) << ” ” ;
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
Stop wasting time,earn money($$$) from your website-Join Now!