liveBook · Manning

Probability distribution

As explained in Learn C++ by Example

A random number distribution is a rule or library component that produces values according to a specified probability distribution.

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

How Learn C++ by Example covers it

How deep it goes
Explains engine-distribution setup, callable use, parameter meanings, templates, and testing, but defers exhaustive distribution details.
Examples
Uses a 1–100 guessing game, 0–4 blob steps, normal values with mean 2.0, and mocked generators for tests.
What it assumes
Assumes basic C++ functions and classes while teaching the engine-distribution relationship and setup from the beginning.
What you can do after
Build it from scratch with a seed, engine, distribution, and operator() call, then substitute distributions or test doubles.
How it connects
Connects random engines, seeding, uniform distribution, normal distribution, templates, polymorphism, and testing.

Also explained in

Compare how 27 books explain probability distribution →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.