Summary
The standard library provides two random-number-related concepts--generator and distribution. Generators are stateful, must be seeded, and produce unsigned integer outputs (raw bits) via operator()(void)
. The two important generator types are std::random_device
, which produces truly random bits, and std::mt19937
, which produces pseudo-random bits.
Distributions are usually stateless, and produce numeric data values via operator()(Gen&)
. The most important distribution type for most programmers will be std::uniform_int_distribution<int>(a,b)
, which produces integers in the closed range [a,b]
. The standard library provides other distributions, such as std::uniform_real_distribution
, std::normal_distribution
, and std::discrete_distribution
, as well as many arcane distributions useful to mathematicians and statisticians.
The one standard algorithm that uses randomness is std::shuffle
, which replaces the old-style std::random_shuffle
. Don't use random_shuffle
in the new code.
Be...