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)[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)

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


xn+1=(axn+c) mod m,un+1=xn+1m.x_{n+1} = (a x_n + c) \bmod m, \qquad u_{n+1} = \frac{x_{n+1}}{m}.


Worked example. Take a=5a = 5, c=3c = 3, m=16m = 16 and seed x0=3x_0 = 3.


x1=(5×3+3) mod 16=18 mod 16=2,u1=216=0.1250,x_1 = (5 \times 3 + 3) \bmod 16 = 18 \bmod 16 = 2, \qquad u_1 = \tfrac{2}{16} = 0.1250,


x2=(5×2+3) mod 16=13,u2=1316=0.8125,x_2 = (5 \times 2 + 3) \bmod 16 = 13, \qquad u_2 = \tfrac{13}{16} = 0.8125,


x3=(5×13+3) mod 16=68 mod 16=4,u3=416=0.2500.x_3 = (5 \times 13 + 3) \bmod 16 = 68 \bmod 16 = 4, \qquad u_3 = \tfrac{4}{16} = 0.2500.


Continuing gives the states 3,2,13,4,7,6,1,8,11,10,5,12,15,14,9,03, 2, 13, 4, 7, 6, 1, 8, 11, 10, 5, 12, 15, 14, 9, 0 and then 33 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 mm, so any serious generator needs an enormous modulus, the Mersenne Twister used by default in many statistical environments has a period of 219937−12^{19937}-1.


Apply the integer recurrence before converting to uu. Intermediate modular reductions are valid: ((axn mod m)+c) mod m=(axn+c) mod m((a x_n \bmod m)+c) \bmod m = (a x_n+c) \bmod m. Omitting the final remainder, or dividing by mm 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)[0,1) has mean 1/21/2 and variance 1/12≈0.08331/12 \approx 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+1u_{n+1} against unu_n. 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/mu_n = x_n/m for n≥1n \ge 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 nn independent draws, the standard error is


SE=p^(1−p^)n∝1n.\mathrm{SE} = \sqrt{\frac{\hat{p}(1-\hat{p})}{n}} \propto \frac{1}{\sqrt{n}}.


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\sqrt{2} \approx 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)X = F^{-1}(U), where FF 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 statistics package produces a stream of numbers between 0 and 1 for use in a simulation. The documentation calls them pseudorandom. What does that term indicate about how they are produced?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A linear congruential generator updates its state by

xn+1=(axn+c) mod m,un+1=xn+1m,x_{n+1} = (a x_n + c) \bmod m, \qquad u_{n+1} = \frac{x_{n+1}}{m},

with a=13a = 13, c=5c = 5, m=64m = 64, and seed x0=7x_0 = 7.

Report u2u_2, the second generated value on the unit interval, to four decimal places.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

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?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors