RNG Algorithm Comparator

Compare pseudo-random number generators (LCG, Mersenne Twister, xorshift) by their period, state space, and statistical quality

Try these examples:

Random Number Generator Comparator

Pseudo-random number generators (PRNGs) produce sequences that appear random but are deterministic. Quality depends on period, state space, and statistical properties.

Algorithm Types

Linear Congruential Generator (LCG)

The simplest PRNG, defined by:

Xn+1=(a⋅Xn+c) mod mX_{n+1} = (a \cdot X_n + c) \bmod m

Fast and simple, with period up to mm (typically 2322^{32} or 2642^{64}). Poor statistical quality in higher dimensions and not suitable for cryptography.

Here is a small one you can see whole: modulus m=2km = 2^k with kk = 8, multiplier aa = 137 and increment cc = 187, started from the seed 42. When mm is a power of 2, the Hull-Dobell theorem says the period is the full mm exactly when cc is odd and a≡1(mod4)a \equiv 1 \pmod 4, so the generator adjusts your choices to fit (aa to the form 4j+14j + 1, cc to an odd number, both reduced mod mm): aa = 137 and cc = 187, and its period is 256.

Successive pairs of an LCG over one full period

1st pair2nd3rd00.5100.20.40.60.81x_k / mx_(k+1) / m

Each dot is a pair of successive outputs (Xk,Xk+1)(X_k, X_{k+1}), divided by mm. Over one period the generator produces every value from 0 to m−1m - 1 once, so these are all the pairs it can ever produce, and they do not fill the square: they lie on a few parallel lines. Knuth's spectral test measures how far apart those lines are: 1/ν21/\nu_2 = 0.06, where ν2\nu_2 = 16.55 is the length of the shortest integer vector (s1,s2)(s_1, s_2) with s1+as2≡0(modm)s_1 + a s_2 \equiv 0 \pmod m. No multiplier can beat ν2\nu_2 = 17.19 for this mm. The poor-multiplier preset (a=129a = 129) puts every pair on lines 0.35 apart. The seed only chooses where the sequence starts (the highlighted first pairs); the lattice is the same for every seed. Real LCGs use m=232m = 2^{32} or more, which packs the lines far closer, but the structure remains, and in nn dimensions Marsaglia showed the points lie on at most (n! m)1/n(n!\,m)^{1/n} hyperplanes: about 2,950 planes in 3D for m=232m = 2^{32}.

Mersenne Twister

A widely-used PRNG with period 219937−12^{19937} - 1 (MT19937 variant), a large state space (624 words), and strong statistical properties. Still not cryptographically secure.

Xorshift

A family of PRNGs based on XOR and shift operations. Period up to 2128−12^{128} - 1 (xorshift128+), small state (2--4 words), very fast, and good statistical properties. Not cryptographically secure.

Configure Your Algorithm

Seed value: 42

Period (in bits): 32

State size (in bytes): 8

Bits per output: 32

Algorithm Metrics

Period length: 4,294,967,296 values

State space: 64 bits

Total outputs per cycle: 4,294,967,296

Memory and Performance

With state size 8 bytes and period 32 bits:

Memory footprint: 8 bytes

Time to exhaust (at 1M ops/sec): 0.00 years

This configuration is suitable for simulation and gaming but not for cryptographic applications. Use a CSPRNG (like ChaCha20) for security.

Understanding Period Length

The period PP is the sequence length before it repeats. Increase 32 and watch the period length grow exponentially:

Period: 4,294,967,296 values

Key considerations:

  • Minimum period: Should far exceed the number of random values you need
  • Birthday paradox: Collisions become likely after P\sqrt{P} outputs
  • State space: Must be large enough to hold all possible internal states

State Space vs. Output Space

Adjust 8 and 32 to explore coverage:

State space: 64 bits = 18,446,744,073,709,552,000 possible states

Output space: 32 bits = 4,294,967,296 possible outputs

Coverage ratio: 4,294,967,296x

Excellent coverage -- each output value can be generated from multiple internal states.

Try it in Code

# PRNG algorithm comparison
period_bits = 32
state_size = 8
bits_per_output = 32

state_space_bits = state_size * 8
period_length = 2 ** period_bits
ops_per_sec = 1_000_000
years_to_exhaust = period_length / ops_per_sec / 31_536_000

print(f"Period: 2^{period_bits}")
print(f"State space: {state_space_bits} bits")
print(f"Output: {bits_per_output} bits/value")
print(f"Memory: {state_size} bytes")
print(f"Years to exhaust: {years_to_exhaust:.2e}")

What to explore next

You might also like