Programs often need a source of random numbers. Prior to the new standard, both C and C++ relied on a simple C library function named rand
. That function produces pseudorandom integers that are uniformly distributed in the range from 0 to a system-dependent maximum value that is at least 32767.
The rand
function has several problems: Many, if not most, programs need random numbers in a different range from the one produced by rand
. Some applications require random floating-point numbers. Some programs need numbers that reflect a nonuniform distribution. Programmers often introduce nonrandomness when they try to transform the range, type, or distribution of the numbers generated by rand
.
The random-number library, defined in the random
header, solves these problems through a set of cooperating classes: random-number engines and random-number distribution classes. These clases are described in Table 17.14. An engine generates a sequence of unsigned
random numbers. A distribution uses an engine to generate random numbers of a specified type, in a given range, distributed according to a particular probability distribution.