Learn
Lesson · Simulation and Variance Reduction · Where the numbers come from

Random Numbers and Inverse-Transform Sampling

A simulation has no randomness in it. It has a recurrence, a seed, and a table: xₙ₊₁ = (a·xₙ + c) mod m produces integers, U = x/m turns one into a fraction in [0, 1), and the sampled value is the first outcome whose cumulative probability reaches U. Both halves are exact, so a run is reproducible from its seed and every count in it is a ratio of whole numbers — and the period of the recurrence decides whether the table is being sampled at all.

xₙ₊₁ = (a·xₙ + c) mod m a = 25173, c = 13849, m = 65536, seed 1 39022, 61087, 20196, 45005 the first four integers, exactly U = x/m = 19511/32768 a fraction, never a decimal F = 1/16, 5/16, 11/16, 15/16, 1 the cumulative table IS the sampler U in [5/16, 11/16) ⟹ the sampled value is 2 full period ⟺ gcd(c, m) = 1, every prime of m divides a − 1, and 4 | m ⟹ 4 | a − 1
One recurrence, one table, and the certificate underneath

Core ideas

What this lesson establishes

Three ideas, and the third is the one that decides whether anything later on this course means anything: the sampler is the table, and a shortcut that does not read the probability column samples a different distribution.

1

A pseudorandom stream is a recurrence, and the seed is all of it

xₙ₊₁ = (a·xₙ + c) mod m is a function, not a source of chance. Fix a, c, m and the seed and every draw in the run is determined — which is a feature, because it is what makes a simulation reproducible, debuggable, and comparable with a second simulation. Nothing on this course would work if the numbers were genuinely unrepeatable.

2

U = x/m is a fraction, and keeping it one keeps everything after it exact

39022/65536 is 19511/32768 and it is not 0.5954. Every statistic taken of a run — a count, a frequency, a sample mean, a sample variance — is then a ratio of whole numbers, and the page can print it in full rather than in a form the reader has to take on trust.

3

The cumulative table is the sampler

Build F(k) = p₀ + ⋯ + p_k and return the first k with U < F(k). The band [F(k−1), F(k)) has width exactly p_k, so the outcome comes out with exactly its own probability. The tempting shortcut — ⌊k·U⌋ + 1 — never looks at the probability column at all, and therefore samples the uniform distribution on k values whatever table is on the page.

The material

A recurrence, a fraction, and the band it falls in

Where the numbers come from, what turns them into outcomes, and the condition that decides whether the stream is long enough to be one.

Definition · Linear congruential generator

A linear congruential generator is the recurrence xₙ₊₁ = (a·xₙ + c) mod m started from a seed x₀. The multiplier a, the increment c and the modulus m fix the whole sequence. Its period is the number of values before it returns to a state it has already been in, and it is at most m.

Discrete Mathematics built this object in “Hashing and Pseudorandom Numbers”, and the lab on this page lets you set all three parameters and watch what they do. What is new here is the use it is put to: this is the only supply of numbers a simulation has, and every figure on the eight pages after this one is a function of it.

Definition · Inverse-transform sampling

Given a distribution on values v₀ < v₁ < ⋯ with probabilities p₀, p₁, …, build the cumulative table F(k) = p₀ + ⋯ + p_k. To sample: draw U = x/m and return the first v_k with U < F(k). The method is called inverse-transform because it is applying the inverse of the cumulative function to a uniform draw.

The correctness argument is one line and is worth having in front of you, because it is what the shortcut breaks. U is uniform on [0, 1), so the probability that it lands in the band [F(k−1), F(k)) is the width of that band, which is p_k by construction. Every outcome therefore comes out with its own probability, and the bands are read off the table rather than assumed.

Two properties of the method matter later and are easy to miss now. It consumes exactly one draw per sampled value, so two runs that sample the same number of things read the same number of draws — which is what makes “Common Random Numbers” possible. And it is monotone in U: a larger U never returns a smaller value. That is the condition “Antithetic Variates” stands on, and it is a property of this sampler rather than a lucky fact.

The comparison is made in whole numbers

U < F(k) is x/m < n/d, which is x·d < n·m, and both sides are integers. Nothing in the sampler ever becomes a decimal, so there is no tie a rounding could decide wrongly. The modulus the rest of this course uses is odd, so U is never exactly 1/2 either, and no comparison on any of these pages has a boundary case at all.

Theorem · Hull-Dobell conditions, as Discrete Mathematics states them

A linear congruential generator has full period m from every seed if and only if gcd(c, m) = 1, every prime dividing m also divides a − 1, and a − 1 is divisible by 4 whenever m is. This course quotes the condition and checks it; the statement belongs to “Hashing and Pseudorandom Numbers”, which states it without proof, and so does this page.

The condition needs c ≠ 0, and the reason is worth a sentence because the generator the rest of this course uses has c = 0. Zero is a fixed point of x ↦ a·x mod m, so a purely multiplicative generator can never visit it and its best possible period is m − 1. Set the lab's generator to a = 16807, c = 0, m = 2147483647 and the first condition reads as a failure: gcd(0, m) = m. That is the theorem declining to apply, not the generator being bad.

Example · A generator with a period of one

Set a = 2, c = 4, m = 16. From seed 1 the stream is 6, 0, 4, 12, 12, 12, …: four values and then a constant forever. All three conditions fail — gcd(4, 16) = 4, and a − 1 = 1 is divisible neither by 2 nor by 4. A sampler fed that stream returns 2, 0, 1, 3, 3, 3, …, which is not a sample of any table; it is one cycle, repeated, with a frequency column that looks like data.

What the run actually produced

a = 25173, c = 13849, m = 65536, seed 1, 200 draws band edges over m: 4096 20480 45056 61440 65536 1/16 5/16 11/16 15/16 1 x = 39022 20480 ≤ 39022 < 45056 value 2 x = 61087 45056 ≤ 61087 < 61440 value 3 x = 20196 4096 ≤ 20196 < 20480 value 1 x = 45005 20480 ≤ 45005 < 45056 value 2 counts 10, 42, 86, 49, 13 mean 413/200 = 2.065 true mean 2 largest gap between an empirical frequency and its probability 11/200, at the value 2

The fourth draw is the one to look at. 45005 is 51 short of 45056, so it lands in the third band rather than the fourth by a margin of fifty-one parts in sixty-five thousand. In whole numbers that is a settled question; in floating point it is a question about the last bit of a double, and the answer decides which outcome the reader is shown.

The gap of 11/200 between the empirical frequency of the value 2 and its probability 6/16 is not an error and not something to apologise for. It is what two hundred draws of this table look like. How large such a gap can honestly be is the subject of the two pages after this one, and the answer is the first thing on this path that arrives as an interval rather than as a number.

Interactive

Inverse-transform sampling, exactly

An integer becomes a fraction, and the cumulative table decides which value it is

The cumulative table is the samplerone integer per draw, turned into U = x/m, matched against F
the first outcome's bandthe secondthe thirdthe fourtheach draw, landing

Method

Sampling a table, by hand

Four steps, and the first is the one that is usually skipped.

1

Check the generator before you use it

Run the Hull-Dobell conditions on a, c and m, or, for a multiplicative generator, check that the modulus is prime and the multiplier is a primitive root. A short period is not a small inaccuracy: the run stops being a sample and becomes a cycle, and nothing downstream complains.

2

Build the cumulative table once, and keep it

F(k) = p₀ + ⋯ + p_k, ending at exactly 1. If the last entry is not 1 the probabilities do not sum to one and the bug is in the model, not in the sampler.

3

Turn each integer into a fraction, not a decimal

U = x/m as a ratio of whole numbers. Every comparison after this is x·d < n·m in the integers, which has no rounding in it and no boundary case to guess at.

4

Return the first band U reaches, and record which one

The first k with U < F(k). Keeping the band alongside the value is what makes a disputed draw checkable afterwards, and it costs nothing.

5

Count the outcomes and compare with the probabilities

Empirical frequency against probability, as fractions, with the difference shown. That difference is the thing the next two pages put a bound on; looking at it now is what makes the bound mean something later.

Streams, bands and periods

Score 0 / 0

Two hundred draws from 1, 4, 6, 4, 1 over 16

The table has mean exactly 2 and variance exactly 1, which is why it is the one five of this course's nine panels open on: every theoretical standard error built on it is a fraction rather than a surd, and the halving of that standard error is a fact rather than a measurement.

table value 0 1 2 3 4 p 1/16 4/16 6/16 4/16 1/16 F 1/16 5/16 11/16 15/16 1 over m = 65536 the band edges are whole numbers edges 4096 20480 45056 61440 65536 stream 39022, 61087, 20196, 45005, 3882, 21259, … draw 1 x = 39022 20480 ≤ x < 45056 value 2 draw 2 x = 61087 45056 ≤ x < 61440 value 3 draw 3 x = 20196 4096 ≤ x < 20480 value 1 draw 4 x = 45005 20480 ≤ x < 45056 value 2 and 45005 is 51 short of the next edge after 200 draws value 0 1 2 3 4 count 10 42 86 49 13 count/n 1/20 21/100 43/100 49/200 13/200 p 1/16 1/4 3/8 1/4 1/16 diff −1/80 −1/25 +11/200 −1/200 +1/400 sample mean 413/200 = 2.065 true mean 2

Every entry in that table is exact. The counts are integers, the frequencies are those integers over two hundred, and the differences are fractions with denominators no larger than four hundred. Nothing here is a measurement of the generator; it is the generator's output, written down.

The sample mean is 413/200, which is 2.065, against a true mean of 2. That is a discrepancy of 13/200 on two hundred draws. Is it large? The honest answer at this point in the course is that nothing on this page can say, and the next page is about the smallest thing that can.

For a faded rehearsal, set the generator to a = 5, c = 3, m = 16 — small enough to walk by hand. The supplied first move is that all three conditions hold, so the period is the full 16 and the stream from seed 1 is 8, 11, 10, 5, 12, 15, 14, 9, 0, 3, 2, 13, …. Work out the first four sampled values by comparing x against the band edges over m = 16 — they are 1, 5, 11, 15, 16 — and say why a modulus of sixteen cannot sample this table faithfully however long you run it, before the panel's frequency column tells you.

Common mistakes

1
Reaching for the index shortcut⌊k·U⌋ + 1 is the first thing most people write and it ignores the probability column entirely. On the table 1, 4, 6, 4, 1 over 16 it returns each of the five outcomes a fifth of the time, so the middle outcome is under-sampled by 3/8 − 1/5 = 7/40 per draw and every expectation computed from the run is wrong by a fixed amount that no number of draws removes.
2
Treating a short period as a small inaccuracyA generator whose period is 1 does not produce a slightly worse sample; it produces no sample. The lab's a = 2, c = 4, m = 16 returns the same outcome one hundred and ninety-seven times out of two hundred and prints a frequency column reading 1, 1, 1, 197, 0, which a reader can mistake for a violently skewed distribution rather than for a dead stream.
3
Storing U as a decimal because the fraction is unreadable19511/32768 is harder to read than 0.5954 and it is the only form in which the comparison against 11/16 has a definite answer. The library's convention is to keep the fraction and print a decimal beside it where a reader needs one — and to say, every time, whether the decimal is the exact value shown short or a genuine rounding.

Check your understanding

✓
Finish when a run's frequency column reads as a fact about the generator as much as about the table.

You should be able to run a linear congruential generator by hand for four steps, check the Hull-Dobell conditions on its parameters and say which fail, build a cumulative table and sample from it by comparing whole numbers, explain in one line why the band [F(k−1), F(k)) gives outcome k its own probability, and say what the index shortcut samples instead.

Note: Everything from here on treats the stream as settled and asks what to do with what it produces. “Monte Carlo and the Standard Error” takes the same two hundred draws, computes the sample mean, and then does the thing this page could not: puts a number on how far that mean can be from the truth.

Kept in this browser only, for you. Nothing is sent anywhere and nothing checks it. Carry it to another device.

Feedback and recommendations

Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.

No recommendations yet for this lesson.