Definition · Linear congruential generatorA 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 samplingGiven 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 themA 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 oneSet 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.