Introduction
Computers often need numbers that look random—for games, simulations, cryptography, or to make user interfaces feel varied. But computers are deterministic machines: given the same inputs, they produce the same outputs. This article explains how computers generate numbers that appear random, the difference between pseudo-random and true random values, common algorithms, where randomness comes from, and practical considerations when you need random-looking numbers.
- Why randomness matters and the basic challenge
- Two broad approaches: pseudo-random vs. true random
- How PRNGs work: simple concepts and common algorithms
- Where seeds come from: the source of unpredictability
- Hardware randomness: capturing physical noise
- Testing randomness: statistical tests and limits
- Practical considerations and trade-offs
- Examples that illustrate the differences
- Common misconceptions
- How to choose a random source in practice
- Future directions and developments
- Conclusion
Why randomness matters and the basic challenge
Random-looking numbers serve many roles: shuffling a playlist, simulating physical processes, choosing test samples, or protecting sensitive data. The key property people expect is unpredictability: the next number should not be easy to guess from previous ones. The challenge for computers is that they follow exact instructions. To get unpredictability, programmers either use deterministic processes that are hard to predict (pseudo-random generators) or harvest unpredictable physical processes (true random sources).
Two broad approaches: pseudo-random vs. true random
Pseudo-random number generators (PRNGs) are algorithms that transform a short initial value, called a seed, into a long sequence of numbers that looks random. If you know the algorithm and the seed, you can reproduce the sequence exactly. PRNGs are fast, repeatable, and widely used where reproducibility or speed matters.
True random number generators (TRNGs), also called hardware random number generators, collect entropy from physical processes that are difficult to predict: electronic noise, radioactive decay, or timing variations. TRNGs produce numbers that are not reproducible from a seed, and they are preferred when unpredictability and security are critical.
How PRNGs work: simple concepts and common algorithms
At their core, PRNGs iterate a mathematical function. Start with a seed value; apply the function to get a new value; use part of that value as the output and feed the rest back to the function. The output sequence depends entirely on the seed and algorithm.
Classic example — Linear Congruential Generator (LCG): An LCG computes the next value using the formula X_{n+1} = (a * X_n + c) mod m, where a, c, and m are fixed integers. LCGs are simple and fast but can have poor statistical properties or short cycles if parameters are not chosen carefully.
Better general-purpose PRNGs: Algorithms like Mersenne Twister, Xorshift, and PCG (Permuted Congruential Generator) are designed to pass many statistical tests for randomness and have long periods (the length before the sequence repeats). Mersenne Twister, for example, has an extremely long period and good distribution properties for simulations—but it is not suitable for cryptographic use because its internal state can be inferred from outputs.
Cryptographically secure PRNGs (CSPRNGs): For security-sensitive applications, PRNGs must resist prediction even when an attacker observes many outputs. CSPRNGs such as those based on block ciphers (e.g., AES in counter mode), stream ciphers, or cryptographic hash functions are designed so that the internal state cannot be feasibly derived from outputs. Examples include Fortuna, ChaCha20-based generators, and system-provided APIs like /dev/urandom on Unix-like systems or CryptGenRandom / BCryptGenRandom on Windows.
Where seeds come from: the source of unpredictability
A PRNG’s unpredictability rests on its seed. If a seed is predictable—like the current time in seconds—an attacker can reproduce the sequence. Good seeding combines multiple sources of entropy. Operating systems gather entropy from events that are hard to predict externally: mouse movements, keyboard timing, disk I/O timing, network packet arrival times, and hardware-generated noise.
Seed management matters. Many systems maintain an entropy pool: a running mix of unpredictable events that feeds CSPRNGs. On first boot or in embedded devices with few external events, lack of entropy can be a problem; designers use hardware entropy sources or delay sensitive operations until enough entropy accumulates.
Hardware randomness: capturing physical noise
Hardware random number generators measure physical processes that are inherently noisy. Some common techniques:
Electronic noise: Thermal noise (Johnson–Nyquist noise) and avalanche noise in diodes produce unpredictable voltage fluctuations. A circuit amplifies and digitizes that noise to produce random bits.
Timing jitter: Small variations in the timing of events (e.g., oscillator jitter, differences in electromagnetic arrival times) can be measured and turned into bits.
Quantum processes: Quantum effects, such as photon arrival times or quantum tunneling, are fundamentally probabilistic and can be harvested for high-quality randomness in quantum random number generators (QRNGs).
Hardware generators are built into many modern CPUs, motherboards, and devices. They are often combined with software mixing to remove bias and increase throughput.
Testing randomness: statistical tests and limits
No finite test can prove a sequence is truly random. Instead, statistical tests look for patterns, biases, or correlations that should not be present if the sequence behaves like a sample from an ideal random process. Common test suites include Dieharder and NIST SP 800-22. They apply many tests—frequency, runs, autocorrelation, and more—to detect weaknesses.
Passing statistical tests is necessary but not sufficient, especially for cryptographic use. A sequence might appear random yet be produced by a predictable algorithm if the seed is known. Conversely, some hardware outputs might fail a particular test because of bias; designers apply post-processing such as whitening or cryptographic hashing to remove bias without reducing entropy.
Practical considerations and trade-offs
Performance: PRNGs are fast and predictable in speed; hardware TRNGs can be slower or produce bursts of entropy. Many systems use a hybrid approach: gather hardware entropy occasionally to re-seed a fast CSPRNG, combining unpredictability with throughput.
Reproducibility: For simulations, testing, or debugging, being able to reproduce a sequence is useful. PRNGs seeded with a known value make experiments repeatable—an essential feature in scientific computing and game development.
Security: For cryptographic keys, nonces, session identifiers, and authentication tokens, use a CSPRNG seeded from high-quality entropy. Poor seeding or non-cryptographic generators can lead to predictable keys and compromised security. Use system-provided secure APIs rather than home-grown randomness for sensitive tasks.
Bias and post-processing: Raw physical sources can be biased (favoring zeros over ones) or exhibit correlations. Simple operations like XORing multiple independent sources can reduce bias, but careful approaches—such as cryptographic hashing or extractors—are preferred to reliably produce uniform bits.
Examples that illustrate the differences
Example 1 — Game simulation: A developer uses a Mersenne Twister seeded with a known seed to make a simulation reproducible. This allows debugging: the same random events happen each time given the same seed.
Example 2 — TLS key generation: A web server needs cryptographic keys. The operating system gathers entropy from hardware sources and user events, feeds a CSPRNG, and produces keys. Predictable seeds here would risk key compromise.
Example 3 — Embedded device: A battery-powered sensor with no user input has few entropy sources. Designers add a hardware noise diode or use manufacturing variations measured at startup to seed a CSPRNG; otherwise cryptographic operations may be insecure.
Common misconceptions
“Random” means unpredictable: In practice, PRNGs are deterministic; only TRNGs are nondeterministic. Whether that matters depends on the application.
All randomness is the same: Not true. Statistical randomness for simulations differs from cryptographic unpredictability. Choose the right tool—fast PRNGs for simulations, CSPRNGs with good entropy for security.
If a sequence passes tests it’s secure: Passing tests helps but doesn’t guarantee security. The seed and algorithm must be appropriate for the threat model.
How to choose a random source in practice
For non-security uses (games, visual effects, Monte Carlo simulations): Use a well-regarded PRNG like Mersenne Twister, PCG, or Xorshift128+. Seed it appropriately if you need reproducibility; otherwise seed it with some entropy like system time combined with process-specific values.
For security-sensitive uses (cryptography, token generation): Use operating system-provided secure random functions (for example, /dev/urandom, getrandom(), or platform APIs). These provide CSPRNG output seeded with system entropy and are maintained with security in mind.
For high-assurance needs (cryptographic key generation, lotteries, certificates): Prefer hardware randomness combined with thorough entropy health checks, and follow best-practice standards or vendor guidance for randomness quality and seeding.
Future directions and developments
Quantum random number generators are becoming more available and can provide high-quality entropy based on fundamental physical unpredictability. At the same time, researchers continue improving PRNG designs to increase performance and robustness, and improving entropy-gathering techniques for constrained devices like the Internet of Things (IoT).
Standards and audits: Cryptographic standards and independent audits help ensure random number implementations meet security expectations. Developers should rely on well-reviewed libraries and platform APIs rather than inventing ad hoc methods.
Conclusion
Computers produce random-looking numbers either by using deterministic algorithms seeded with unpredictable values (PRNGs) or by harvesting noise from physical processes (TRNGs). PRNGs are fast and reproducible; CSPRNGs provide security when seeded with good entropy. Hardware generators supply nondeterministic entropy but often need conditioning. The right choice depends on needs for speed, reproducibility, and security—so use well-tested algorithms and system-provided entropy sources when unpredictability matters.

