A probability distribution specifies how a random-number component produces values. In C++, it works with a random engine: the engine supplies randomness, while the distribution shapes the results. section 3.2 section 3.4
Generate values by following this small setup:
- Seed a random engine, commonly with std::random_device. section 3.2.2
- Choose a distribution whose parameters describe the desired result. section 3.2 section 6.2.1
- Call distribution(engine) to draw a value; reuse the setup for later draws. section 3.2.2
A uniform integer distribution gives values in a range with approximately equal proportions. Other distributions express different patterns: a normal distribution centers values around a mean, while Bernoulli, binomial, and Poisson distributions model other kinds of outcomes. section 3.2.2 section 6.2.1
Guessing game and blob race
A number-guessing game can draw an integer from 1 through 100, then use it as the hidden number. A racing blob can draw a step from 0 through 4 on every move. Replacing the uniform distribution with a normal distribution changes the blob's behavior: 2.0 becomes the mean and 1.0 the standard deviation, not minimum and maximum bounds. section 3.2.2 section 6.2 section 6.2.1
Distributions share a callable interface that accepts a generator, even though they do not share a common base class. A class template can therefore store any compatible generator and distribution, making randomized behavior replaceable and testable with mocked callables. section 6.2.1 section 6.2.1
Listing 3.9 Generating a single random number
int some_random_number()
{
std::random_device rd; #1
std::mt19937 engine(rd()); #2
std::uniform_int_distribution<int> dist(1, 100); #3
return dist(engine); #4
}
Shows the complete minimal pipeline: seed a Mersenne Twister engine, construct a uniform integer distribution, and draw one value. section 3.2.2


























