3. Random Number Generation

Every random variate in a simulation is built from i.i.d. U(0,1) numbers; these notes cover where those numbers come from and what makes a generator good.

Motivation. Consider simulating an M/M/1M/M/1 queue (Markovian, i.e. exponential, interarrival times; exponential service times; one server). We must repeatedly generate X∼Exp(λ)X \sim \text{Exp}(\lambda), whose c.d.f. is F(x)=1−e−λxF(x) = 1 - e^{-\lambda x}, x≥0x \ge 0. This can be done from a single uniform random number:

  1. Generate U∼U(0,1)U \sim U(0,1).
  2. Return X=−1λln⁡UX = -\dfrac{1}{\lambda}\ln U.

Why it works: setting U=F(X)U = F(X) and solving for XX gives X=F−1(U)=−1λln⁡(1−U)X = F^{-1}(U) = -\frac{1}{\lambda}\ln(1-U); since 1−U∼U(0,1)1-U \sim U(0,1) as well, we may use ln⁡U\ln U instead. Then

P(X≤x)=P(−1λln⁡U≤x)=P(U≥e−λx)=1−e−λx=F(x).P(X \le x) = P\left(-\tfrac{1}{\lambda}\ln U \le x\right) = P\left(U \ge e^{-\lambda x}\right) = 1 - e^{-\lambda x} = F(x).

The same idea applies to other distributions: every random variate in a simulation is built from i.i.d. U(0,1)U(0,1) numbers.

Goal of this chapter. Produce a sequence of i.i.d. U(0,1)U(0,1) random numbers, i.e. numbers with p.d.f.

f(x)={1,0≤x≤1,0,otherwise.f(x) = \begin{cases} 1, & 0 \le x \le 1, \\ 0, & \text{otherwise.} \end{cases}

Three alternative sources of “random” numbers

1. True random numbers (from physical experiments)

  • Draw numbered balls from an urn.
  • Electrical circuits: a hardware random number machine, e.g. ERNIE (Electronic Random Number Indicator Equipment).
  • Disadvantages:
    • Not reproducible. Reproducibility matters in simulation: it makes debugging easier, and it gives sharper comparisons when two alternative systems are run on the same random inputs.
    • Difficult to implement.
    • Slow.

2. Pseudo-random numbers — a completely deterministic sequence that is statistically indistinguishable from a true random sequence. This is what simulation uses in practice, and what the rest of the chapter is about.

3. Quasi-random numbers (used in Monte Carlo integration rather than in simulation)

An integral over [0,1][0,1] is an expectation under U(0,1)U(0,1), because the U(0,1)U(0,1) p.d.f. equals 11 there:

∫01g(x) dx=∫01g(x)⋅1 dx=E[g(X)],X∼U(0,1).\int_0^1 g(x)\,dx = \int_0^1 g(x)\cdot 1\,dx = \mathbf{E}[g(X)], \qquad X \sim U(0,1).

So generate X1,…,Xn∼i.i.d.U(0,1)X_1, \dots, X_n \overset{\text{i.i.d.}}{\sim} U(0,1) and estimate the integral by

gˉ(n)=1n∑i=1ng(Xi)  ⟶  E[g(X)].\bar{g}(n) = \frac{1}{n}\sum_{i=1}^n g(X_i) \;\longrightarrow\; \mathbf{E}[g(X)].

By the CLT the error of this estimator is of order 1/n1/\sqrt{n}. The idea of quasi-random numbers is to replace the random sequence by a deterministic sequence that covers [0,1][0,1] more evenly; this improves the error rate to order (ln⁡n)/n(\ln n)/n. Such sequences are not i.i.d., so they are useless as simulation inputs, but for integration the even coverage is exactly what helps.

g(x) 0 1 i.i.d. U(0,1) — clumps and gaps quasi-random — covers [0,1] evenly

3.1. Random Number Generators (RNGs)

Desired properties of an RNG

  1. Uniformity: the sequence should appear to be uniformly distributed on (0,1)(0,1).
  2. Independence: terms in the sequence should not be correlated.
  3. Reproducibility: one must be able to reproduce a particular stream of random numbers (e.g. from a seed).
  4. Fast, with low memory usage.
  5. Long period. A deterministic generator eventually loops. The segment of non-repeating numbers is called a cycle, and its length is the period of the RNG.
flowchart LR
    u1(("u₁")) --> u2(("u₂")) --> u3(("u₃")) --> d[". . ."] --> uP(("u_P")) --> u1

The sequence repeats after PP terms, so the period must be far longer than the number of random numbers a simulation will consume.

3.2. Linear Congruential Generators (LCGs)

An LCG produces integers by the recursion

zi=(a zi−1+c) mod m,i=1,2,…z_i = (a\, z_{i-1} + c) \bmod m, \qquad i = 1, 2, \dots

where z0z_0 is the initial seed, aa is the multiplier, cc is the increment, and mm is the modulus. All four are non-negative integers, and x mod yx \bmod y is the remainder when xx is divided by yy.

3.2.1. From integers to uniforms.

Since ziz_i is a remainder after division by mm,

z0,z1,z2,⋯∈{0,1,…,m−1}.z_0, z_1, z_2, \dots \in \{0, 1, \dots, m-1\}.

Dividing by mm scales these into the unit interval:

ui=zim∈[0,1),i=0,1,2,…u_i = \frac{z_i}{m} \in [0, 1), \qquad i = 0, 1, 2, \dots

The uiu_i are the “random numbers” delivered to the simulation; the ziz_i are the generator’s internal state.

Example. Take m=16m = 16, a=5a = 5, c=3c = 3, z0=7z_0 = 7:

ii 0 1 2 3 4 ⋯\cdots 16
ziz_i 7 6 1 8 11 ⋯\cdots 7
uiu_i 0.4375 0.375 0.0625 0.5 0.6875 ⋯\cdots 0.4375

e.g. z1=(5⋅7+3) mod 16=38 mod 16=6z_1 = (5\cdot 7 + 3) \bmod 16 = 38 \bmod 16 = 6. Here z16=z0z_{16} = z_0, so the sequence has period 16=m16 = m.

Observations.

  1. Values on a grid. The uiu_i can only take the mm values {0,1m,2m,…,m−1m}\left\{0, \tfrac{1}{m}, \tfrac{2}{m}, \dots, \tfrac{m-1}{m}\right\}: rational numbers on an equally spaced grid, not a genuinely continuous U(0,1)U(0,1) variable. In practice one takes mm large, typically m≥109m \ge 10^9; then the grid is so fine that ui≈U(0,1)u_i \approx U(0,1) for all practical purposes.
  2. Looping behaviour. Since ziz_i depends only on zi−1z_{i-1}, as soon as some earlier value reappears the whole sequence after it repeats. There are only mm possible values, so a value must recur within mm steps. The length of the repeating segment is the period of the LCG, and by this argument the period is at most mm. An LCG whose period equals mm is called a full-period LCG: it visits every value 0,1,…,m−10, 1, \dots, m-1 exactly once per cycle.
  3. Determinism. The whole sequence is fixed by (a,c,m,z0)(a, c, m, z_0). Unrolling the recursion gives the closed form (for a≠1a \ne 1)

    zi=[aiz0+c ai−1a−1] mod m,z_i = \left[a^i z_0 + c\,\frac{a^i - 1}{a - 1}\right] \bmod m,

    so ziz_i can be computed directly from the seed without generating z1,…,zi−1z_1, \dots, z_{i-1}.

Example. m=16m = 16, a=5a = 5, c=3c = 3, z0=7z_0 = 7, so zi=(5zi−1+3) mod 16z_i = (5 z_{i-1} + 3) \bmod 16:

ii 0 1 2 3 4 ⋯\cdots 16 17
ziz_i 7 6 1 8 11 ⋯\cdots 7 6
uiu_i 0.438 0.375 0.063 0.5 0.688 ⋯\cdots 0.438 0.375

The first repeat is z16=z0=7z_{16} = z_0 = 7, so this LCG has full period 1616.

What if z0=6z_0 = 6 or z0=1z_0 = 1 instead? The recursion is the same, so we get the same cycle entered at a different point — e.g. 6,1,8,11,…,7,6,…6, 1, 8, 11, \dots, 7, 6, \dots — again with period 1616. In a full-period LCG all mm values lie on a single cycle, so any choice of seed produces the entire cycle, in some order; the seed only picks the starting point.

For a non-full-period LCG, the values split into several cycles and the period depends on the seed:

Example. c=0c = 0, a=13a = 13, m=64m = 64, so zi=13zi−1 mod 64z_i = 13 z_{i-1} \bmod 64.

a. z0=4z_0 = 4:   4→52→36→20→4\;4 \to 52 \to 36 \to 20 \to 4, period 44.
b. z0=2z_0 = 2:   2→26→18→42→34→58→50→10→2\;2 \to 26 \to 18 \to 42 \to 34 \to 58 \to 50 \to 10 \to 2, period 88.

Notice that in (a) every value is a multiple of 44, and in (b) every value is ≡2(mod4)\equiv 2 \pmod 4: multiplying by 1313 never changes the power of 22 dividing ziz_i, so the sequence is trapped in its residue class. This is exactly what the theorem below rules out.

3.2.2. Necessary and sufficient conditions for a full-period LCG

Theorem (Hull–Dobell). The LCG zi=(azi−1+c) mod mz_i = (a z_{i-1} + c) \bmod m has full period mm if and only if

  1. cc and mm are relatively prime (their only common divisor is 11);
  2. every prime qq that divides mm also divides a−1a - 1;
  3. if 44 divides mm, then 44 divides a−1a - 1.

Why three conditions. A full-period LCG must visit every one of 0,1,…,m−10, 1, \dots, m-1. The test is to look at the sequence “through a coarser lens”: for a divisor dd of mm, watch only zi mod dz_i \bmod d. If the coarse sequence misses some residue mod dd, the full sequence misses some value mod mm. Condition 1 removes the obstruction coming from the increment cc; condition 2 removes the obstruction coming from the multiplier aa, one prime q∣mq \mid m at a time; condition 3 is needed because the prime 22 misbehaves in a way no other prime does, so the lens d=4d = 4 must be checked separately.

Why each condition is necessary.

  • Condition 1. Suppose cc and mm share a divisor d>1d > 1. Modulo dd the recursion becomes zi≡azi−1(modd)z_i \equiv a z_{i-1} \pmod d, so if z0≡0(modd)z_0 \equiv 0 \pmod d then every zi≡0(modd)z_i \equiv 0 \pmod d: the sequence never leaves the multiples of dd. In the example above c=0c = 0, so gcd⁡(c,64)=64\gcd(c, 64) = 64 and full period is impossible for any seed.
  • Condition 2. Let qq be a prime dividing mm. Modulo qq the map is x↦ax+cx \mapsto ax + c. If a≢1(modq)a \not\equiv 1 \pmod q, then 1−a1 - a is invertible mod qq and the map has a fixed point x∗≡c (1−a)−1(modq)x^* \equiv c\,(1-a)^{-1} \pmod q. Any seed with z0≡x∗(modq)z_0 \equiv x^* \pmod q stays in that residue class forever, so the period is less than mm. Hence we need a≡1(modq)a \equiv 1 \pmod q, i.e. q∣a−1q \mid a - 1; then the map mod qq is x↦x+cx \mapsto x + c, which does cycle through all qq residues because gcd⁡(c,q)=1\gcd(c, q) = 1 by condition 1.
  • Condition 3. Condition 2 with q=2q = 2 only gives aa odd, i.e. a≡1a \equiv 1 or 3(mod4)3 \pmod 4. If a≡3(mod4)a \equiv 3 \pmod 4, then applying the map twice mod 44 gives x↦9x+3c+c≡x+4c≡x(mod4)x \mapsto 9x + 3c + c \equiv x + 4c \equiv x \pmod 4, so the sequence mod 44 has period at most 22 and cannot visit all four residues. (Concretely, a=3a = 3, c=1c = 1, m=4m = 4, z0=0z_0 = 0 gives 0,1,0,1,…0, 1, 0, 1, \dots) So 4∣m4 \mid m forces a≡1(mod4)a \equiv 1 \pmod 4.

Sufficiency — that the three conditions together guarantee period exactly mm — is the harder direction and is what Hull and Dobell proved; it is not needed for the course beyond the statement.

Check on the earlier example (m=16m = 16, a=5a = 5, c=3c = 3): gcd⁡(3,16)=1\gcd(3, 16) = 1; the only prime dividing 1616 is 22, and 2∣4=a−12 \mid 4 = a - 1; 4∣164 \mid 16 and 4∣44 \mid 4. All three hold, so full period 1616, as observed.

3.2.3. Two types of LCGs

Type 1: Mixed generators (LCGs with c>0c > 0)

We want mm large (observation 1). The standard choice is

m=2b,m = 2^b,

where bb is the number of bits available in a machine word. On a 32-bit system one bit is used for the sign, so b=31b = 31 and m=231m = 2^{31}. The reason for a power of two is speed: computing x mod 2bx \bmod 2^b costs nothing, because it is just keeping the lowest bb bits of xx (integer overflow does it automatically).

With m=2bm = 2^b the only prime dividing mm is 22, so the three Hull–Dobell conditions specialize as follows:

  1. gcd⁡(c,2b)=1\gcd(c, 2^b) = 1   ⟺  \;\Longleftrightarrow\; cc is odd.
  2. 2∣a−12 \mid a - 1   ⟺  \;\Longleftrightarrow\; aa is odd.
  3. 4∣2b4 \mid 2^b, hence 4∣a−14 \mid a - 1   ⟺  \;\Longleftrightarrow\; a≡1(mod4)a \equiv 1 \pmod 4.

Item 3 implies item 2, so item 2 is redundant, but it is a common mistake to stop there: a=3,7,11,…a = 3, 7, 11, \dots are odd yet give short periods. The rule for m=2bm = 2^b is therefore simply

c oddanda≡1(mod4).c \text{ odd} \quad\text{and}\quad a \equiv 1 \pmod 4 .

A good mixed LCG: m=231m = 2^{31}, a=314159269a = 314159269, c=453806245c = 453806245.
Check: cc is odd, and a−1=314159268=4⋅78539817a - 1 = 314159268 = 4 \cdot 78539817, so a≡1(mod4)a \equiv 1 \pmod 4. Full period 231≈2.1×1092^{31} \approx 2.1 \times 10^9.

Type 2: Multiplicative generators (LCGs with c=0c = 0)

zi=a zi−1 mod m,sozi=aiz0 mod m.z_i = a\, z_{i-1} \bmod m, \qquad\text{so}\qquad z_i = a^i z_0 \bmod m .

  1. A multiplicative LCG can no longer have full period. Condition 1 of Hull–Dobell fails, since gcd⁡(c,m)=gcd⁡(0,m)=m≠1\gcd(c, m) = \gcd(0, m) = m \ne 1. Concretely: 00 is a fixed point (a⋅0=0a \cdot 0 = 0), so the sequence can never pass through 00 without staying there forever. A useful generator must therefore avoid 00 altogether, and the best it can do is cycle through {1,2,…,m−1}\{1, 2, \dots, m-1\}: the maximum possible period is m−1m - 1.
  2. Period m−1m - 1 is achievable if aa and mm are chosen carefully.
  3. Choose mm prime, specifically the largest prime less than 2b2^b. (For b=31b = 31 this is 231−1=21474836472^{31} - 1 = 2147483647, which happens to be prime.) Primality matters because of the next point.
  4. When is the period exactly m−1m - 1? Suppose mm is prime and z0≠0z_0 \ne 0. The sequence returns to its start at step ll when

    alz0≡z0(modm).a^l z_0 \equiv z_0 \pmod m .

    Because mm is prime and z0≢0z_0 \not\equiv 0, we can cancel z0z_0, leaving

    al≡1(modm),i.e.m∣al−1.a^l \equiv 1 \pmod m, \quad\text{i.e.}\quad m \mid a^l - 1 .

    So the period is the smallest l≥1l \ge 1 with al≡1(modm)a^l \equiv 1 \pmod m, and it does not depend on the seed. By Fermat’s little theorem am−1≡1(modm)a^{m-1} \equiv 1 \pmod m always, so this smallest ll divides m−1m - 1. The period equals m−1m - 1 exactly when the smallest such ll is m−1m - 1:

    min⁡{ l≥1:al−1≡0(modm) }=m−1.\min\{\, l \ge 1 : a^l - 1 \equiv 0 \pmod m \,\} = m - 1 .

    An aa with this property is called a primitive element modulo mm (or primitive root). Hence: if mm is prime and aa is a primitive element modulo mm, the multiplicative LCG has period m−1m - 1 for every seed z0≠0z_0 \ne 0.

Such generators — multiplicative (c=0c = 0) with a prime modulus mm and a primitive multiplier aa — are called prime modulus multiplicative LCGs (PMMLCGs). The name lists the ingredients: prime modulus is point 3, multiplicative is c=0c = 0, and the period m−1m - 1 comes from point 4.

Example. m=7m = 7 (prime).

  • a=3a = 3: 31=3, 32=2, 33=6, 34=4, 35=5, 36=1(mod7)3^1 = 3,\ 3^2 = 2,\ 3^3 = 6,\ 3^4 = 4,\ 3^5 = 5,\ 3^6 = 1 \pmod 7. The first ll with 3l≡13^l \equiv 1 is l=6=m−1l = 6 = m - 1, so 33 is primitive and the generator zi=3zi−1 mod 7z_i = 3 z_{i-1} \bmod 7 has period 66: from z0=1z_0 = 1 it runs 1,3,2,6,4,5,1,…1, 3, 2, 6, 4, 5, 1, \dots
  • a=2a = 2: 21=2, 22=4, 23=1(mod7)2^1 = 2,\ 2^2 = 4,\ 2^3 = 1 \pmod 7. Here l=3≠6l = 3 \ne 6, so 22 is not primitive; the generator zi=2zi−1 mod 7z_i = 2 z_{i-1} \bmod 7 splits {1,…,6}\{1,\dots,6\} into two cycles of length 33: 1,2,41, 2, 4 and 3,6,53, 6, 5.

A good PMMLCG. m=231−1=2147483647m = 2^{31} - 1 = 2147483647 (prime), a=75=16807a = 7^5 = 16807 or a=630360016a = 630360016. Both multipliers are primitive elements modulo mm, so the period is m−1=231−2≈2.1×109m - 1 = 2^{31} - 2 \approx 2.1 \times 10^9.

A bad multiplicative LCG: RANDU. m=231m = 2^{31}, a=216+3=65539a = 2^{16} + 3 = 65539, c=0c = 0. It was the default generator on IBM mainframes in the 1960s–70s, and many early simulation papers were based on it. Its period is long enough (2292^{29}, the maximum m/4m/4 for a power-of-two modulus), but it has a severe defect: because a=216+3a = 2^{16} + 3,

a2=232+6⋅216+9≡6a−9(mod231),a^2 = 2^{32} + 6 \cdot 2^{16} + 9 \equiv 6a - 9 \pmod{2^{31}},

and multiplying by ziz_i gives the exact linear relation

zi+2≡6 zi+1−9 zi(mod231).z_{i+2} \equiv 6\, z_{i+1} - 9\, z_i \pmod{2^{31}} .

So every value is determined by the two before it via a fixed formula with tiny coefficients. Geometrically, all consecutive triples (ui,ui+1,ui+2)(u_i, u_{i+1}, u_{i+2}) lie on just 1515 parallel planes in the unit cube, instead of filling it — a gross violation of independence (property 2 of a good RNG). Any simulation that uses three successive numbers together (e.g. to generate a point in 3-D) inherits this structure.

The lesson of RANDU: a long period is necessary but not sufficient. Type 1 and Type 2 generators both need to be tested for uniformity and independence, which is the subject of the next section.

3.3. Other Kinds of Generators

More general congruential generators. Everything so far has the shape

zi=g(zi−1,zi−2,…,z0) mod m,ui=zim,z_i = g(z_{i-1}, z_{i-2}, \dots, z_0) \bmod m, \qquad u_i = \frac{z_i}{m},

for some function gg of the previous values. An LCG is the special case g(zi−1)=azi−1+cg(z_{i-1}) = a z_{i-1} + c. Other choices of gg give other generators.

Multiple recursive generators (MRGs). Take gg linear in the last qq values:

zi=(a1zi−1+a2zi−2+⋯+aqzi−q) mod m,z_i = (a_1 z_{i-1} + a_2 z_{i-2} + \cdots + a_q z_{i-q}) \bmod m,

where a1,…,aqa_1, \dots, a_q are integer constants. This is the multiplicative LCG with qq lags instead of one (q=1q = 1 gives back zi=a1zi−1 mod mz_i = a_1 z_{i-1} \bmod m).

  • Advantage: a huge period. The state of the generator is now the qq-tuple (zi−1,…,zi−q)(z_{i-1}, \dots, z_{i-q}), of which there are mqm^q possibilities. The all-zero tuple is a fixed point (as 00 was for the multiplicative LCG), so the maximum period is mq−1m^q - 1, and this is attained for suitable a1,…,aqa_1, \dots, a_q when mm is prime. With m≈231m \approx 2^{31} and q=3q = 3 this is about 293≈10282^{93} \approx 10^{28}.
  • Disadvantage: qq multipliers to choose instead of one, and the theory for choosing them well is harder. The output sequence still looks like an LCG’s — a linear recurrence with lattice structure — just in qq dimensions rather than one.

Other choices of gg.

  • Quadratic congruential generator: g(zi−1)=azi−12+bzi−1+cg(z_{i-1}) = a z_{i-1}^2 + b z_{i-1} + c, i.e.

    zi=(azi−12+bzi−1+c) mod m.z_i = (a z_{i-1}^2 + b z_{i-1} + c) \bmod m .

    This is nonlinear in zi−1z_{i-1}, but it still depends on the previous value only, so the period is still at most mm.

  • Fibonacci generator: g(zi−1,zi−2)=zi−1+zi−2g(z_{i-1}, z_{i-2}) = z_{i-1} + z_{i-2}, i.e.

    zi=(zi−1+zi−2) mod m.z_i = (z_{i-1} + z_{i-2}) \bmod m .

    This is the MRG with q=2q = 2 and a1=a2=1a_1 = a_2 = 1. It is very fast (no multiplication) but statistically poor. To see why, look at three consecutive outputs ui−2,ui−1,uiu_{i-2}, u_{i-1}, u_i with ui=(ui−1+ui−2) mod 1u_i = (u_{i-1} + u_{i-2}) \bmod 1. If the sum does not wrap past 11, then uiu_i is larger than both ui−1u_{i-1} and ui−2u_{i-2}; if it does wrap, ui=ui−1+ui−2−1u_i = u_{i-1} + u_{i-2} - 1 is smaller than both. So uiu_i is never the middle value of the three. For i.i.d. uniforms each of the six orderings of three values should occur with probability 1/61/6; the Fibonacci generator produces two of them with probability 00.

Practical issues

  1. Period. The period of the RNG should be at least an order of magnitude larger than the total number of random numbers the simulation will consume. Otherwise the sequence wraps around and later parts of the run reuse the same numbers as earlier parts, destroying independence.
  2. Streams. A stream is a segment of the RNG’s cycle starting from a chosen seed. Use a different stream for each replication of a simulation, with the seeds spaced far enough apart that the streams do not overlap. Non-overlapping streams from one long-period generator then behave like independent generators.
flowchart LR
    s1["stream 1<br/>seed z₀⁽¹⁾"] --> s2["stream 2<br/>seed z₀⁽²⁾"] --> s3["stream 3<br/>seed z₀⁽³⁾"] --> d[". . ."] --> s1
  1. Separate streams for separate inputs. Use different streams to simulate quantities that should be independent — e.g. one stream for interarrival times and another for service times. Besides keeping the inputs independent, this synchronizes the random numbers: when two alternative systems are simulated, each can be fed the same interarrival stream and the same service stream, so any difference in the output is due to the systems and not to the random inputs (the “sharper comparison” mentioned at the start of the chapter).

3.4. Testing RNGs

Two kinds of tests:

  • Theoretical tests (number theory). These analyze the generator’s parameters (a,c,m)(a, c, m) mathematically and say something about the entire cycle — a global property — without generating a single uiu_i. Example: the spectral test, which measures how far apart the parallel hyperplanes are on which all kk-tuples (ui,…,ui+k−1)(u_i, \dots, u_{i+k-1}) of an LCG lie (recall RANDU’s 15 planes).
  • Empirical tests (statistics). Generate a finite sample u1,…,unu_1, \dots, u_n and test the null hypothesis

    H0: u1,u2,… are i.i.d. U(0,1).H_0:\ u_1, u_2, \dots \text{ are i.i.d. } U(0,1).

    These are only local tests: they examine the part of the cycle that was actually generated, so a defect elsewhere in the cycle goes undetected. H0H_0 has two parts, hence two families of tests: tests for uniformity and tests for independence.

3.4.1. Test for uniformity (frequency test / χ2\chi^2 test)

Goal. Check whether the uiu_i appear to be uniformly distributed over [0,1][0, 1].

Steps.

  1. Divide [0,1][0, 1] into kk subintervals of equal length 1/k1/k.
  2. Generate nn random numbers u1,…,unu_1, \dots, u_n from the RNG. Rule of thumb: k≥100k \ge 100 and n/k≥5n/k \ge 5 (at least 5 expected observations per interval, so that the approximation in step 4 is accurate).
  3. Count the number of uiu_i falling in the jj-th interval; call it fjf_j, so f1+⋯+fk=nf_1 + \cdots + f_k = n.
f₁ = 2 f₂ = 4 f₃ = 2 · · · fk = 3 0 1/k 2/k 1
  1. Compute the test statistic

    χ2=∑j=1k(fj−nk)2nk.\chi^2 = \sum_{j=1}^{k} \frac{\left(f_j - \dfrac{n}{k}\right)^2}{\dfrac{n}{k}} .

    Under H0H_0 each interval should receive about n/kn/k points, so χ2\chi^2 measures the total squared deviation of the observed counts from that. When nn is large, χ2\chi^2 approximately follows a chi-square distribution with k−1k-1 degrees of freedom.
  2. Reject H0H_0 at significance level α\alpha if

    χ2>χk−1, α2,\chi^2 > \chi^2_{k-1,\,\alpha},

    where the threshold χk−1, α2\chi^2_{k-1,\,\alpha} is defined by P ⁣(χk−12>χk−1, α2)=αP\!\left(\chi^2_{k-1} > \chi^2_{k-1,\,\alpha}\right) = \alpha (upper α\alpha-quantile, as with zαz_\alpha and tα,n−1t_{\alpha, n-1}). Large χ2\chi^2 means the counts are too uneven to be uniform; only the upper tail is used.

What is the chi-square distribution? If X1,…,XdX_1, \dots, X_d are i.i.d. N(0,1)N(0,1), then

Q=∑i=1dXi2Q = \sum_{i=1}^{d} X_i^2

is a chi-square random variable with dd degrees of freedom, written Q∼χd2Q \sim \chi^2_d.

Why χ2\chi^2 is approximately χk−12\chi^2_{k-1} under H0H_0.

Assume H0H_0 is true. Each uiu_i lands in interval jj with probability 1/k1/k, independently of the others, so the count fjf_j is binomial:

fj∼Bin ⁣(n,1k),P(fj=r)=(nr)(1k)r(1−1k)n−r,f_j \sim \text{Bin}\!\left(n, \tfrac{1}{k}\right), \qquad P(f_j = r) = \binom{n}{r}\left(\tfrac{1}{k}\right)^{r}\left(1 - \tfrac{1}{k}\right)^{n-r},

E[fj]=nk,Var⁡(fj)=nk(1−1k).\mathbf{E}[f_j] = \frac{n}{k}, \qquad \operatorname{Var}(f_j) = \frac{n}{k}\left(1 - \frac{1}{k}\right).

By the CLT (a binomial is a sum of nn Bernoullis), the standardized count is approximately standard normal for large nn:

Zj=fj−n/knk(1−1k)  ≈  N(0,1).Z_j = \frac{f_j - n/k}{\sqrt{\dfrac{n}{k}\left(1 - \dfrac{1}{k}\right)}} \;\approx\; N(0,1).

If the ZjZ_j were independent, ∑j=1kZj2\sum_{j=1}^k Z_j^2 would be χk2\chi^2_k by the definition above. Writing this sum out,

∑j=1kZj2=kk−1∑j=1k(fj−n/k)2n/k=kk−1 χ2,i.e.χ2=k−1k∑j=1kZj2.\sum_{j=1}^{k} Z_j^2 = \frac{k}{k-1}\sum_{j=1}^{k}\frac{(f_j - n/k)^2}{n/k} = \frac{k}{k-1}\,\chi^2, \qquad\text{i.e.}\qquad \chi^2 = \frac{k-1}{k}\sum_{j=1}^{k} Z_j^2 .

But the ZjZ_j are not independent: the counts satisfy f1+⋯+fk=nf_1 + \cdots + f_k = n, one linear constraint, so only k−1k-1 of them are free. The precise result (Pearson) is that this constraint costs exactly one degree of freedom and the factor (k−1)/k(k-1)/k is exactly what compensates: χ2≈χk−12\chi^2 \approx \chi^2_{k-1}, not χk2\chi^2_k. The heuristic to remember is: kk approximately normal terms, one constraint, k−1k-1 degrees of freedom.

0 χ²k−1, α density of χ²k−1 area = α reject H₀ →

The threshold χk−1,α2\chi^2_{k-1,\alpha} cuts off an upper tail of area α\alpha. Under H0H_0 the statistic lands in that tail with probability only α\alpha, so landing there is taken as evidence against uniformity.

Example. Twenty numbers are generated from an RNG:

0.10, 0.32, 0.76, 0.54, 0.08, 0.23, 0.67, 0.89, 0.41, 0.15,0.10,\ 0.32,\ 0.76,\ 0.54,\ 0.08,\ 0.23,\ 0.67,\ 0.89,\ 0.41,\ 0.15,

0.95, 0.60, 0.28, 0.73, 0.49, 0.05, 0.37, 0.82, 0.00, 0.91.0.95,\ 0.60,\ 0.28,\ 0.73,\ 0.49,\ 0.05,\ 0.37,\ 0.82,\ 0.00,\ 0.91 .

Are these from a uniform distribution on [0,1][0,1], at significance level α=0.05\alpha = 0.05?

H0: the numbers are i.i.d. U(0,1).H_0:\ \text{the numbers are i.i.d. } U(0,1).

Step 1. Divide [0,1][0,1] into kk subintervals. With only n=20n = 20 numbers we cannot meet k≥100k \ge 100; take k=4k = 4, which at least satisfies n/k=5n/k = 5. (This is a hand-sized illustration — a real test would use thousands of numbers.)

Step 2. n=20n = 20 numbers are already generated.

Step 3. Count how many fall in each subinterval. Expected count per interval: n/k=20/4=5n/k = 20/4 = 5.

jj interval numbers in it fjf_j n/kn/k fj−n/kf_j - n/k
1 [0,0.25)[0, 0.25) 0.10, 0.08, 0.23, 0.15, 0.05, 0.00 6 5 +1+1
2 [0.25,0.5)[0.25, 0.5) 0.32, 0.41, 0.28, 0.49, 0.37 5 5 00
3 [0.5,0.75)[0.5, 0.75) 0.54, 0.67, 0.60, 0.73 4 5 −1-1
4 [0.75,1)[0.75, 1) 0.76, 0.89, 0.95, 0.82, 0.91 5 5 00
total 20 20 00

Step 4. Compute the test statistic:

χ2=∑j=14(fj−n/k)2n/k=(+1)2+02+(−1)2+025=25=0.4.\chi^2 = \sum_{j=1}^{4} \frac{(f_j - n/k)^2}{n/k} = \frac{(+1)^2 + 0^2 + (-1)^2 + 0^2}{5} = \frac{2}{5} = 0.4 .

Step 5. Compare with the threshold. Degrees of freedom k−1=3k - 1 = 3, and from the chi-square table

χ3, 0.052=7.815.\chi^2_{3,\,0.05} = 7.815 .

Since χ2=0.4≤7.815\chi^2 = 0.4 \le 7.815, we do not reject H0H_0 at the 5%5\% level: there is no evidence that these numbers are non-uniform.

Remark. “Do not reject” is not “proved uniform.” First, the test only looks at the marginal distribution — a sequence like 0.1,0.2,…,0.9,0.1,0.2,…0.1, 0.2, \dots, 0.9, 0.1, 0.2, \dots would pass the uniformity test easily while being obviously non-random, which is why a test for independence is also needed (§3.4.2). Second, with n=20n = 20 the test has very little power; the counts would have to be wildly off to produce a χ2\chi^2 above 7.87.8.

Serial tests (higher-dimensional χ2\chi^2 tests)

The one-dimensional test only checks the marginal distribution. The serial test checks a consequence of the full hypothesis:

If the uiu_i are i.i.d. U(0,1)U(0,1), then the non-overlapping dd-tuples

(u1,…,ud),(ud+1,…,u2d),…(u_1, \dots, u_d),\quad (u_{d+1}, \dots, u_{2d}),\quad \dots

are uniformly distributed over the hypercube [0,1]d[0,1]^d.

This is an implication, not an equivalence: A⇒BA \Rightarrow B, so failing BB disproves AA. That is enough for a test — if the tuples are not uniform on the cube, the numbers are not i.i.d. U(0,1)U(0,1).

Case d=2d = 2.

  1. Divide the square [0,1]2[0,1]^2 into k2k^2 subsquares of equal size.
  2. Generate nn pairs U1=(u1,u2)U_1 = (u_1, u_2), U2=(u3,u4)U_2 = (u_3, u_4), …, Un=(u2n−1,u2n)U_n = (u_{2n-1}, u_{2n}). Note each uu is used once: 2n2n numbers give nn pairs.
  3. Let fijf_{ij} be the number of UU’s falling in subsquare (i,j)(i,j). The expected count per square is n/k2n/k^2, so

    χ2=∑i=1k∑j=1k(fij−nk2)2nk2  ≈  χk2−12\chi^2 = \sum_{i=1}^{k}\sum_{j=1}^{k} \frac{\left(f_{ij} - \dfrac{n}{k^2}\right)^2}{\dfrac{n}{k^2}} \;\approx\; \chi^2_{k^2 - 1}

    (k2k^2 cells, one constraint ∑i,jfij=n\sum_{i,j} f_{ij} = n, hence k2−1k^2 - 1 degrees of freedom — same counting as in §3.4.1).
  4. Reject H0H_0 at level α\alpha if χ2>χk2−1, α2\chi^2 > \chi^2_{k^2-1,\,\alpha}; see [[Review of Prob and Stats#2.6.3. Statistical tables (zz, tt, $ chi 2$)]].
0 1 1 u₂ᵢ₋₁ u₂ᵢ k² subsquares, each of area 1/k² expected count per square: n/k² points scattered evenly ⇒ consistent with H₀

Why this is an indirect test of independence. The one-dimensional test cannot see dependence at all: a sequence can have a perfectly uniform histogram and still be highly predictable. The serial test looks at successive values jointly, so such structure shows up as clumping in the square.

0 1 uᵢ uᵢ₊₁ each coordinate alone looks uniform on [0,1] … … but the pairs hug the diagonal: uᵢ₊₁ ≈ uᵢ, so they are not independent. The 1-D test passes; the serial test catches it.

Limitations.

  • Indirect. Passing the serial test does not prove independence; it only means no dependence was detected at the resolution used. RANDU, for instance, passes comfortably in d=2d = 2 and fails only in d=3d = 3, where its 15 planes appear.
  • Costly for large dd. The number of cells is kdk^d, and the rule n/kd≥5n/k^d \ge 5 forces n≥5kdn \ge 5k^d — exponential in dd. With k=10k = 10 and d=5d = 5 that is already 500,000500{,}000 tuples, i.e. 2.52.5 million random numbers. In practice one uses a small kk for larger dd, or switches to a theoretical test such as the spectral test, which examines all dimensions at once without sampling.

3.4.2. Test for independence (runs test)

What “runs” means. A run up is an unbroken subsequence of uiu_i’s that is monotonically increasing; the run ends as soon as the next number is smaller. The sequence is thus chopped into maximal ascending pieces, and the test looks at how long those pieces are. The name is literal: how far does the sequence “run” upward before turning around?

The idea is that the lengths of these runs have a known distribution when the uiu_i are i.i.d., and this distribution depends on the order of the numbers, not just on their values. That is exactly what the frequency test cannot see.

Example (the same 20 numbers as in §3.4.1). Mark ↗\nearrow where ui+1>uiu_{i+1} > u_i and ∣| where a run ends:

0.10↗0.32↗0.76  ∣  0.54  ∣  0.08↗0.23↗0.67↗0.89  ∣  0.41  ∣  0.15↗0.95  ∣  0.10 \nearrow 0.32 \nearrow 0.76 \;\big|\; 0.54 \;\big|\; 0.08 \nearrow 0.23 \nearrow 0.67 \nearrow 0.89 \;\big|\; 0.41 \;\big|\; 0.15 \nearrow 0.95 \;\big|\;

0.60  ∣  0.28↗0.73  ∣  0.49  ∣  0.05↗0.37↗0.82  ∣  0.00↗0.910.60 \;\big|\; 0.28 \nearrow 0.73 \;\big|\; 0.49 \;\big|\; 0.05 \nearrow 0.37 \nearrow 0.82 \;\big|\; 0.00 \nearrow 0.91

The run lengths are 3,1,4,1,2,1,2,1,3,23, 1, 4, 1, 2, 1, 2, 1, 3, 2 (they sum to n=20n = 20, as they must).

The counts. Let

ri={number of runs up of length i,i=1,2,…,5,number of runs up of length≥6,i=6.r_i = \begin{cases} \text{number of runs up of length } i, & i = 1, 2, \dots, 5, \\ \text{number of runs up of length} \ge 6, & i = 6. \end{cases}

Lengths ≥6\ge 6 are pooled into one category because they are rare, so their individual counts would be too small for the approximation to hold — the same reason the frequency test wants n/k≥5n/k \ge 5 per cell.

For the example: r1=4r_1 = 4, r2=3r_2 = 3, r3=2r_3 = 2, r4=1r_4 = 1, r5=r6=0r_5 = r_6 = 0.

Expected counts. Under H0H_0 the expected number of runs of each length is E[ri]≈n bi\mathbf{E}[r_i] \approx n\,b_i, where the bib_i are universal constants (they do not depend on the distribution, only on the ordering being random):

b=(16,  524,  11120,  19720,  295040,  1840)≈(0.1667,  0.2083,  0.0917,  0.0264,  0.0058,  0.0012).b = \left(\tfrac{1}{6},\; \tfrac{5}{24},\; \tfrac{11}{120},\; \tfrac{19}{720},\; \tfrac{29}{5040},\; \tfrac{1}{840}\right) \approx (0.1667,\; 0.2083,\; 0.0917,\; 0.0264,\; 0.0058,\; 0.0012).

Note ∑ibi=1/2\sum_i b_i = 1/2: a random sequence of length nn splits into about n/2n/2 runs up. Runs of length 22 are the most common, not length 11.

The test statistic.

R=1n∑i=16∑j=16aij (ri−nbi)(rj−nbj),R = \frac{1}{n}\sum_{i=1}^{6}\sum_{j=1}^{6} a_{ij}\,(r_i - n b_i)(r_j - n b_j),

where the aija_{ij} are constants tabulated in the textbook (Law & Kelton, p. 408); the matrix is symmetric, with entries in the thousands — the first row begins 4529.4, 9044.9, 13568,…4529.4,\ 9044.9,\ 13568, \dots

Why the double sum, unlike the χ2\chi^2 statistic of §3.4.1? Because the rir_i are strongly correlated with one another: a long run uses up numbers that then cannot start short runs, so an excess of r4r_4 forces a deficit elsewhere. A plain sum of squared deviations ∑i(ri−nbi)2/(nbi)\sum_i (r_i - nb_i)^2 / (nb_i) would therefore not have a chi-square distribution. The matrix (aij)(a_{ij}) is (up to scaling) the inverse of the covariance matrix of the vector (r1,…,r6)(r_1, \dots, r_6), and multiplying by it removes those correlations — this is the general recipe for turning a correlated normal vector into a chi-square variable.

Decision rule. For large nn (the textbook recommends n≥4000n \ge 4000), under

H0: the ui are i.i.d.H_0:\ \text{the } u_i \text{ are i.i.d.}

RR has approximately a chi-square distribution with 6 degrees of freedom — not 6−1=56 - 1 = 5, because here no constraint like ∑fj=n\sum f_j = n ties the six counts together. Reject H0H_0 at significance level α\alpha if

R>χ6, α2,R > \chi^2_{6,\,\alpha},

with the threshold read from [[Review of Prob and Stats#2.6.3. Statistical tables (zz, tt, $ chi 2$)]]: e.g. χ6, 0.052=12.592\chi^2_{6,\,0.05} = 12.592.

(The 20-number example cannot be carried further: with n=20≪4000n = 20 \ll 4000 the chi-square approximation is meaningless. It only illustrates how the rir_i are counted.)

In practice, perform the runs test first. Two reasons:

  1. The frequency test’s null distribution was derived assuming independence — that is what made fjf_j binomial in §3.4.1. If the numbers are dependent, the χ2\chi^2 threshold is not valid, so testing uniformity first can give a meaningless answer.
  2. Dependence is the more damaging defect. A generator whose output is uniform but correlated will quietly bias any simulation, whereas mild non-uniformity is often visible and correctable.

3. Random Number Generation
http://example.com/2026/09/08/2026-09-08-random-number-generation/
Author
Wind_like
Posted on
September 8, 2026
Licensed under