Glossary Math & Statistics

Pseudo-random number

A pseudo-random number comes from a deterministic algorithm that starts from a seed and produces a sequence that looks random and passes statistical tests.

Last reviewed:

AdSense Placeholder
Slot: header_reference_page

In more detail

A pseudo-random number generator (PRNG) keeps an internal state, updates it with a fixed formula and outputs numbers derived from it. The simplest kind, a linear congruential generator, computes x(n+1) = (a × x(n) + c) mod m. Python's random module uses the Mersenne Twister, whose sequence repeats only after 2¹⁹⁹³⁷ − 1 values. The same seed always gives the same sequence, which is useful for reproducible tests and simulations.

Why it matters

Ordinary PRNGs are predictable: after observing enough outputs, an attacker can reconstruct the state and predict every future value. For passwords, keys, session tokens and lottery draws use a cryptographically secure generator fed by operating-system Entropy instead, such as Python's secrets module or crypto.getRandomValues() in browsers, not random or Math.random(). See How Randomness and Fair Draws Work.

Example

>>> import random
>>> random.seed(42)
>>> random.random()
0.6394267984578837
>>> random.seed(42)
>>> random.random()          # same seed, same value
0.6394267984578837
>>> import secrets
>>> secrets.token_hex(16)    # different every run

A toy generator x → (5x + 3) mod 16 started at 7 gives 6, 1, 8, 11, 10, 5, 12, 15, 14, 9, 0, 3, 2, 13, 4, 7 and then repeats: every possible value appears once per cycle, so the 17th output is predictable from the first.

AdSense Placeholder
Slot: footer_leaderboard