In this chapter we introduce the *C++ random library*. The ability to generate ‘random numbers’ is useful in applications such as the simulation of natural and artificial phenomena, sampling, numerical analysis, decision making, cryptography and computer programming (for an account of random numbers, their construction and applications, see for example Knuth, 1997).

Traditionally, C++ has had limited support for the production of random numbers. It adopted the corresponding functionality from the C library, for example the function rand() that produces *pseudo-random numbers*. This function has so many shortcomings and it is not recommended for serious applications. Instead, we use *random number engines* that produce unpredictable (random) bits, thus ensuring that the likelihood of producing a 0 bit is the same as the likelihood of producing a 1 bit. Two popular engines are the *Mersenne Twister* and *linear congruential* algorithms. The C++11 engines deliver uniformly distributed numbers but they should not be used as raw data in applications. Instead, they are used as input to functionality in C++11 to transform uniform variates to random variates of a given *statistical distribution*, for example the normal and chi-squared distributions. C++11 supports 20 distributions that are classified into five categories.

The C++ random number library is easy to use and we shall show some examples in this chapter. ...

Start Free Trial

No credit card required