Computer Generated Random Numbers
Computer Generated Random Numbers: Seeds, Periods, and Checking the Output
Deterministic numbers that behave like random ones
A computer has no source of randomness in its arithmetic. What it has is a deterministic recurrence: a rule that turns the current internal state into the next one, together with a way of converting each state into a number in [0,1).
Given the same starting state, the seed, the same stream comes out every time, on every machine running the same algorithm.
Running mean of a linear congruential stream (a = 1103515245, c = 12345, m = 2^31, seed 2026)
These numbers are called pseudorandom because they are not random at all; they are built to pass the tests we would apply to random numbers. That is a modest claim and a sufficient one for simulation.
Two consequences follow directly from determinism, and both are useful rather than regrettable: the stream is exactly reproducible, and because the state space is finite the stream must eventually repeat. The length of that cycle is the generator's period.
A generator you can compute by hand
A linear congruential generator updates an integer state by
Worked example. Take a=5, c=3, m=16 and seed x0=3.
Continuing gives the states 3,2,13,4,7,6,1,8,11,10,5,12,15,14,9,0 and then 3 again: every residue appears exactly once, so this generator has the maximum possible period of 16. Two things are worth noticing. The output looks unpatterned while being completely determined.
And the period is bounded by m, so any serious generator needs an enormous modulus, the Mersenne Twister used by default in many statistical environments has a period of 219937−1.
Apply the integer recurrence before converting to u. Intermediate modular reductions are valid: ((axnmodm)+c)modm=(axn+c)modm. Omitting the final remainder, or dividing by m before the integer state is finalized, can change the specified stream.
Seeding
Setting a seed makes a run reproducible, which is what allows a result to be checked, debugged, or published with its code.
The rule of thumb is: set the seed once, at the top of the script, and let the generator run forward from there. Every subsequent draw is then determined, so re-running the script reproduces the entire study.
The common error is resetting the same seed inside a loop. Each iteration then restarts the generator from the same state and produces the same draws, so a study of 500 replicates becomes one replicate repeated 500 times.
The output looks fine, the numbers vary within a replicate, and the variance between replicates collapses to zero, which quietly destroys exactly the quantity the study was measuring.
For a generator with a documented seeding procedure, an ordinary valid user seed generally need not be large, prime, or unusual. Some algorithms do have forbidden or degenerate internal states, so use the library's supported initializer and seed range rather than constructing internal states directly.
Checking the output
Never assume a stream is behaving. Cheap checks catch real mistakes.
- Mean and variance. A uniform variable on [0,1) has mean 1/2 and variance 1/12≈0.0833. A sample mean far from 0.5, or a variance far from 0.0833, warrants checking the scaling and generator behavior alongside ordinary sampling variation; an unusual sample alone does not identify a cause.
- Histogram, or a chi-square goodness-of-fit test. Bin the draws into equal intervals and compare the counts with what uniformity predicts.
- Serial structure. Plot un+1 against un. Uniform marginal behaviour does not imply independence, and a generator can pass a histogram test while its successive pairs lie on a small number of lines.
- Running summaries. Plot a running mean against the number of draws and check that it settles toward the expected value rather than drifting. The chart applies this check to the first 2000 draws of the generator named in its title, with un=xn/m for n≥1: the running mean is 0.5133 after 200 draws and stays within 0.01 of 0.5 from 400 draws onward.
How fast a simulation estimate converges
For a proportion estimated from n independent draws, the standard error is
Precision improves with the square root of effort, so quartering the error requires sixteen times the draws.
Going from 1000 to 4000 draws halves the standard error; going from 1000 to 2000 reduces it only by a factor of 2≈1.41. Budget accordingly, and note that this error is Monte Carlo error, uncertainty from using a finite simulation, and is entirely separate from any uncertainty in the model being simulated.
Turning uniforms into other things, carefully
Most other distributions are built from uniforms. The inverse transform method uses X=F−1(U), where F is the target cumulative distribution function.
Integer ranges need more care than they appear to. Reducing a uniform integer with a remainder operation is only exact when the number of possible values is an exact multiple of the number of outcomes wanted.
Otherwise some outcomes are reachable from one more starting value than others, and they come out slightly too often.
The bias is small when the range is large, and it is real, systematic, and invisible in a histogram of a modest sample, which is why it survives so long in practice.
A linear congruential generator updates its state by
with a=13, c=5, m=64, and seed x0=7.
Report u2, the second generated value on the unit interval, to four decimal places.
A simulation study runs 500 replicates in a loop. Each replicate should use its own fresh draws, and the study as a whole must be exactly reproducible so that a reviewer re-running the script obtains the same results.
Which seeding practice achieves both requirements?