Concepts / Random Number Distribution

Random Number Distribution

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

  • Programming

Why Programs Need Unpredictable Values

Imagine you are 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 first question a programmer faces is not which numbers to pick, but how a program should produce unpredictable values at all, and how to shape those values so they fit what the task needs.

Work in testing, games, simulation, and security all relies on random numbers, and these areas differ a great deal from one another. The standard library for C++ accordingly provides a wide selection of random number generators, collected in the <random> header. That variety is a clue to the rest of this article: a generator is not a single lump of behavior. It is a combination of parts that can be chosen and arranged in different ways.

What do you think happens?

If a generator is built from parts that can be chosen and arranged differently, what do you think those two parts might be?

Reveal answer
Answer:

An engine, which produces a sequence of random or pseudo-random values, and a distribution, which maps those values into a range.

The next section names both parts and explains what each one does. Keep your own guess in mind as you read it, so you can check how close it was.

The Two Parts of a Generator

Definition

A random number generator consists of two parts: an engine and a distribution. The engine is responsible for producing a sequence of random or pseudo-random values. The distribution takes those values and maps them into a mathematical distribution over a range. Neither part does the other's job, and the generator as a whole depends on both working together.

Definition

A distribution describes the pattern that random values follow. A generator can produce values, but a program often needs those values to follow a particular pattern. Some tasks need every possible result to be equally likely, while others need results to cluster around a typical value. Choosing the right distribution is part of generating random numbers well, because the same generator can give very different-looking results depending on which distribution shapes its output.

Engine Versus Distribution Roles

The two parts are easiest to separate by asking what each one decides. The engine decides which value comes next in the sequence. The distribution decides what shape the outcomes take and which range they fall into. A useful way to keep this straight is to remember that the engine supplies raw material and the distribution shapes it.

Engine: roleproduces the sequenceDistribution: rolemaps values into a rangeEngine: outputrandom or pseudo-randomvaluesDistribution: outputvalues with a set shape andbounds
Which part produces raw randomness, which part shapes that randomness into a pattern, and how the two parts differ in what they output.

Read the comparison as two columns. The engine column ends with raw values, which are not yet shaped for any particular use. The distribution column begins with those raw values and ends with values that have a pattern and fit a range. The two columns are not interchangeable: removing the distribution would leave you with the raw sequence, and removing the engine would leave the distribution with nothing to transform.

Tracing One Value Through the Generator

Follow a single value from start to finish. Suppose 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. The data moves in one direction: from the engine into the distribution, and then out as the result.

Engineproduces the next value inits sequenceRaw valuerandom or pseudo-random,not yet a die faceDistributionmaps the value into therange 1 to 6Final resultthe die roll the game usesnext valueentersyields
What happens to a raw value produced by the engine, and how the distribution transforms it into a final value in the target range.
Worked example

Tracing a die roll from engine to result

A game asks the generator for a fair die roll, a whole number from 1 to 6. Trace what happens to the value on its way to the final result.

  1. 1
    Step 1: request

    The game asks the generator for a value. The request is handled by the generator as a whole.

  2. 2
    Step 2: engine produces a raw value

    The engine supplies the next value in its sequence. This value is random or pseudo-random, and at this point it is not yet a die face. The engine has no say in the range 1 to 6.

  3. 3
    Step 3: distribution receives the value

    The distribution takes the raw value from the engine. This is the first point where the value is shaped for the task.

  4. 4
    Step 4: distribution maps into the range

    The distribution maps the raw value into the range from 1 to 6. The distribution sets the bounds, so the result can only be one of those whole numbers.

  5. 5
    Step 5: final result

    The mapped value is returned as the die roll. The game uses this value directly, with no further processing needed.

Result

A whole number from 1 to 6, produced by the engine and shaped by the distribution.

The Uniform Integer Distribution

The uniform integer distribution, named uniform_int_distribution in the source, produces integers that are all equally likely over a specified range. The word uniform describes the lack of preference. Within the specified range, no integer is favored over any other.

1equal share2equal share3equal share4equal share5equal share6equal share
Why every value in the range has the same chance of appearing, and how each value maps to an equal share of outcomes.

The diagram lists each integer in the range from 1 to 6 as its own cell, and every cell carries the same equal share. No cell is larger or smaller than another. This is why the uniform distribution is the baseline for many random choices: when every outcome is treated the same, the result is fair in the sense that no value stands out.

In the real world

A card game that deals from a shuffled deck, or a test suite that needs any valid input in a range to be tried with the same likelihood, both rely on the idea that no value should be favored. The uniform integer distribution expresses that expectation directly.

The Normal Distribution and the Bell Curve

The normal distribution, named normal_distribution in the source, is also known as the bell curve. It concentrates values toward the middle. The bell curve tells you where values concentrate. Rather than treating every value in the range equally, the normal distribution gives the central region of its range the most weight, with values becoming less common as they move toward the edges.

Weight across rangesame for every valueWeight across rangemost at the centerShape of outcomesflatShape of outcomesbell curve
How the shape of outcomes under a normal distribution, clustered around a center, differs from the flat spread of a uniform distribution.

Place the two distributions next to each other and the contrast is clear. Both work over a specified range, but they distribute attention across that range in opposite ways. The uniform distribution spreads its likelihood evenly, so every integer is equally likely. The normal distribution concentrates likelihood in the middle and gives less to the edges. In the uniform case every value carries the same weight, while in the normal case the weight depends on position within the range.

What do you think happens?

If you had to make a random value near the middle of a range more likely than values near the edges, which distribution would you pick?

Reveal answer
Answer:

The normal distribution, the bell curve, which gives the central region of its range the most weight.

The uniform distribution would give the middle and the edges equal likelihood, so it would not meet this need.

The Exponential Distribution and Growth

The exponential distribution, named exponential_distribution in the source, represents exponential growth and is the third distribution the source names. When a pattern is exponential, each step does not add a fixed amount but multiplies the previous level, so the values climb faster and faster.

The source offers the exponential idea as a conceptual picture of growth: a sequence of increasing values in which each value builds on the one before it. It is not a set of particular numbers, and the source does not give further detail on how the exponential distribution's outcomes are spread over a range. When you meet this distribution, the important thing to recognize is the link to exponential growth, which is what distinguishes it from the uniform and normal cases.

Choosing Among the Three Distributions

Choosing a distribution means asking what pattern the task needs. The three distributions cover three different expectations, summarized in the table below. The table only records what the source states about each distribution, so it is a guide to the features that point toward each choice rather than a complete specification.

DistributionSource namePattern of outcomesPoints to choose it when
Uniform integeruniform_int_distributionEvery integer in the range is equally likelyEvery possible result must be treated the same, such as a fair die roll
Normal (bell curve)normal_distributionValues concentrate toward the middle and become less common toward the edgesResults should cluster around a typical value
Exponentialexponential_distributionRepresents exponential growth, with values climbing as each step multiplies the previous levelThe situation is described by exponential growth

Three distributions named in the source and the features that point to each

MEDIUM

A team is writing a simulation of customers arriving at a shop. Most customers arrive at a typical rate, with fewer arriving at unusually high or low rates. A second team needs a fair die roll for a board game. Which distribution fits each team, and which feature of each situation points to your choice?

Hints
  • Ask first whether every outcome should be equally likely, or whether results should cluster around a typical value.
  • The die roll needs every face from 1 to 6 to be equally likely, which points to the uniform integer distribution.
  • Results that cluster around a typical value point to the normal distribution. The exponential distribution is tied to exponential growth, which the shop description does not mention.

Mistakes Beginners Make With Distributions

  • Common mistake

    Treating the engine's output as the final answer

    What it looks like

    Using the raw value from the engine directly as a die roll without any mapping.

    Why it fails

    The engine produces the sequence but does not decide the range. The raw value is not yet a die face.

    Do this instead

    Pass the engine's value through the distribution, which maps it into the range the task needs.

  • Common mistake

    Assuming the normal distribution is just a more random version of the uniform distribution

    What it looks like

    Choosing the normal distribution for a fair die roll because it seems more varied.

    Why it fails

    The normal distribution concentrates weight in the middle, so the values are not equally likely. The uniform distribution is the one in which no value is favored.

    Do this instead

    Pick the uniform integer distribution when every outcome must be equally likely, and the normal distribution when results should cluster around a typical value.

  • Common mistake

    Choosing a distribution by its name without checking the task's pattern

    What it looks like

    Using the exponential distribution for any random task because it sounds advanced.

    Why it fails

    The source ties the exponential distribution to exponential growth. A task without that pattern does not need it.

    Do this instead

    Start from the pattern the task needs, then select the distribution that produces that pattern.

Checking Your Understanding of the Pipeline

Before moving on, trace one more value through the generator in your own words. Start at the engine, pass the raw value to the distribution, and end at the result in the target range. If you can name what the engine decides, what the distribution decides, and which distribution gives a flat spread versus a centered one, you have the core of this article.

Key Takeaways

  • A random number generator has two cooperating parts: the engine produces a sequence of random or pseudo-random values, and the distribution maps those values into a range with a specified pattern.
  • Data flows from the engine to the distribution, and the distribution's mapping sets the range and shape of the final result.
  • The uniform integer distribution (uniform_int_distribution) makes every integer in its range equally likely, so no value is favored.
  • The normal distribution (normal_distribution), or bell curve, concentrates values toward the middle, while the exponential distribution (exponential_distribution) represents exponential growth.
  • Choose the distribution by first identifying the pattern the task needs: equal likelihood, clustering around a typical value, or exponential growth.

Related concepts

More concepts starting with R