Mathwords logoMathwords

Pseudorandom Number — Definition, Formula & Example

A pseudorandom number is a number produced by a fixed computer rule that only looks random. Start the rule from the same seed and you get exactly the same sequence again.

A pseudorandom number generator (PRNG) is a deterministic function ff on a finite state space, together with an output map. Starting from a seed X0X_0, it produces the sequence Xn+1=f(Xn)X_{n+1} = f(X_n). Because the state space is finite, the sequence is eventually periodic. A good generator has a long period, and its scaled outputs Un∈[0,1)U_n \in [0,1) pass statistical tests for being independent draws from the uniform distribution on [0,1)[0,1).

Key Formula

Xn+1=(aXn+c) mod m,Un=XnmX_{n+1} = (aX_n + c) \bmod m, \qquad U_n = \frac{X_n}{m}
Where:
  • XnX_n = the n-th integer in the sequence, with 0 ≤ XnX_n < m
  • X0X_0 = the seed, the starting value you choose
  • aa = the multiplier, a positive integer
  • cc = the increment, a nonnegative integer
  • mm = the modulus, which is the number of possible states
  • UnU_n = the output scaled to the interval [0, 1)

Worked Example

Problem: A linear congruential generator uses a=5a = 5, c=3c = 3, m=16m = 16, and seed X0=7X_0 = 7. Find the next three pseudorandom numbers, scaled to [0,1)[0,1).
Step 1: Apply the rule to the seed. Divide by 16 and keep the remainder.
X1=(5⋅7+3) mod 16=38 mod 16=6X_1 = (5 \cdot 7 + 3) \bmod 16 = 38 \bmod 16 = 6
Step 2: Feed each output back into the rule.
X2=(5⋅6+3) mod 16=33 mod 16=1,X3=(5⋅1+3) mod 16=8X_2 = (5 \cdot 6 + 3) \bmod 16 = 33 \bmod 16 = 1, \qquad X_3 = (5 \cdot 1 + 3) \bmod 16 = 8
Step 3: Divide each value by m=16m = 16 to get numbers in [0,1)[0,1).
U1=616=0.375,U2=116=0.0625,U3=816=0.5U_1 = \tfrac{6}{16} = 0.375, \quad U_2 = \tfrac{1}{16} = 0.0625, \quad U_3 = \tfrac{8}{16} = 0.5
Answer: The integer sequence is 6, 1, 8, and the scaled outputs are 0.375, 0.0625, 0.5. Rerunning from seed 7 always gives these same values. This generator has gcd⁡(3,16)=1\gcd(3,16) = 1, and a−1=4a - 1 = 4 is divisible by 2 and by 4. By the Hull–Dobell theorem, those conditions mean it cycles through all 16 states before it repeats.

Why It Matters

Monte Carlo simulation, bootstrap resampling in statistics, and randomized algorithms all rely on long streams of pseudorandom uniforms. Those uniforms are then transformed into other distributions. Reproducibility matters too: fixing the seed lets you or a reviewer rerun a simulation and get identical results.

Common Mistakes

Mistake: Treating any pseudorandom generator as safe for passwords, encryption keys, or lottery draws.
Correction: An attacker who sees a few outputs of a simple generator, such as a linear congruential generator, can often recover its parameters and predict every later value. For security, use a cryptographically secure generator. Reserve ordinary PRNGs for simulation and statistics.

Related Terms