Concepts / Random Number Engine

Random Number Engine

A random number generator consists of two parts: an engine and a distribution.

  • Programming

Why Programs Need Unpredictable Values

Imagine building a card game, a test suite for a login system, a simulation of customers arriving at a shop, and a security feature that issues temporary codes. Each of these needs values that cannot be predicted in advance. The source names testing, games, simulation, and security as the kinds of work that rely on random numbers.

The first question a programmer faces is not which numbers to pick. It is how a program should produce unpredictable values at all, and how to shape those values so they fit what the task needs. Those two questions correspond to the two parts that make up a random number generator.

The standard library for C++ provides a wide selection of random number generators, collected in the random header. That variety is a clue to how the whole topic works: a generator is not a single lump of behavior, but a combination of parts that can be chosen and arranged in different ways.

The Two Parts of a Generator

Definition

A random number generator consists of two parts: an engine and a distribution. The engine produces a sequence of random or pseudo-random values. The distribution takes those values and maps them into a mathematical distribution over a range.

The two parts cooperate. Neither part does the other's job, and the generator as a whole depends on both working together. The diagram below shows how the two parts are related to the generator as a whole.

Random NumberGeneratorthe wholeEngineproduces the sequenceDistributionmaps values into a rangeconsists ofconsists of
What are the two parts that together form a random number generator, and how are they related to the generator as a whole?

The Engine: Producing the Sequence

The engine is the source of the sequence. Each time you ask it for a value, it supplies the next one in its sequence. The source describes these as random or pseudo-random values, so the engine's output is what the rest of the generator works from.

The Distribution: Shaping the Output

The distribution receives values from the engine and maps them into a range. This is the stage where a raw value becomes something a program can use directly. The source says the mapping is into a mathematical distribution in a range, so the distribution is the part that gives the output its shape and bounds.

Produces the sequenceMaps values into arangeRandom orpseudo-random valuesGives shape andbounds
How do the engine and the distribution differ in what they do: one produces raw random output, while the other shapes that output into the desired form?

A helpful way to separate the roles: the engine answers the question what comes next in the sequence, while the distribution answers the question what form and bounds should this value have. The two questions are different, so the two parts are different.

Tracing One Value from Start to Finish

The source describes the direction of data: the engine's sequence enters the distribution, and the distribution's output is the set of values in a range. The arrows show direction of data only. Follow a single value through that pipeline with a concrete case.

Worked example

A fair six-sided die roll

A game needs a fair six-sided die roll, meaning a whole number from 1 to 6. The program asks a generator for a value. Trace that value from the engine to the final result.

  1. 1
    Ask the generator

    The program requests one value from the generator.

  2. 2
    Engine produces the next value

    Following the two-part structure, the engine first produces the next value in its sequence. At this point the value is raw and is not yet a die face.

  3. 3
    Pass to the distribution

    The raw value moves from the engine into the distribution.

  4. 4
    Distribution maps into the range

    The distribution maps that raw value into the range from 1 to 6, giving the output its bounds.

  5. 5
    Final result

    The result is the die roll the game uses.

Result

A whole number from 1 to 6, produced in two stages: the engine supplies the raw value, and the distribution shapes it into the range.

Engineproduces the next valueRaw valuenot yet a die faceDistributionmaps into the rangeResult in rangea whole number 1 to 6emitspassed toyields
What happens to one raw random value as it leaves the engine, gets passed to the distribution, and ends up as a final number within the requested range?

Predicting What the Engine Returns

What do you think happens?

In the die example, is the value the engine returns already a number from 1 to 6?

  • Yes, the engine already returns a die face

  • No, the value is raw and still needs the distribution

Reveal answer
Answer:

No, the value is raw and still needs the distribution

The engine only produces the next value in its sequence. It is not concerned with the range. The distribution is the stage that maps that raw value into the range from 1 to 6.

Mistakes Beginners Make with the Two Parts

  • Common mistake

    Treating the engine's output as the final answer

    What it looks like

    Assuming the value straight from the engine is already a die face from 1 to 6.

    Why it fails

    The engine produces the sequence of random or pseudo-random values. It is not concerned with deciding the range.

    Do this instead

    Pass the raw value through the distribution, which maps it into the range the task needs.

  • Common mistake

    Thinking the distribution creates the randomness

    Why it fails

    The source says the distribution takes values from the engine and maps them. The distribution works from the engine's sequence rather than producing that sequence itself.

    Do this instead

    Credit the engine with producing the sequence and the distribution with shaping it.

  • Common mistake

    Assuming the two parts can be merged into one step without losing the separation of roles

Practice: Trace a Different Range

MEDIUM

A test suite needs a random login code that is a whole number from 1 to 100. Describe which part of the generator produces the raw sequence value and which part maps it into the 1 to 100 range. Then state what the engine would need to change in order to produce values for this new range.

Hints
  • Recall the sentence: the engine is concerned with producing the sequence, not with deciding what range the final numbers should fall into.
  • Separate the question of which value comes next from the question of what range the value must fall into.

Key Takeaways

  1. 1
    A random number generator is built from two cooperating parts: an engine and a distribution.
  2. 2
    The engine produces a sequence of random or pseudo-random values and supplies the next one each time it is asked.
  3. 3
    The distribution takes the engine's values and maps them into a range, giving the output its shape and bounds.
  4. 4
    Data flows from the engine to the distribution, and the final result is the value the distribution produces within the range.

At a glance

  • A random number generator consists of two parts: an engine and a distribution.
  • The engine produces the sequence of random or pseudo-random values, and it does not decide the final range.
  • The distribution maps the engine's values into a range, which gives the output its shape and bounds.
  • A single value moves from the engine, to the distribution, to a final result within the requested range.

Related concepts

More concepts starting with R