Random number generators (RNGs) are not truly random because the ones built into computers and everyday software are deterministic algorithms: given the same starting value (seed), they always produce the same sequence of numbers. Only specialized hardware that measures physical processes, like radioactive decay or atmospheric noise, can generate genuine randomness, and even then, the output is often post-processed.
What People Mean by “Random”
In everyday language, “random” means unpredictable and lacking any pattern. But in computing, randomness is defined by two different standards: statistical randomness (the sequence passes tests for uniform distribution and independence) and unpredictability (an observer cannot guess the next number even if they know the algorithm and previous outputs). Most software RNGs achieve statistical randomness but fail unpredictability. For example, the sequence 2, 4, 6, 8 is statistically unlikely but perfectly predictable. A good pseudo-random number generator (PRNG) produces numbers that look random to statistical tests, but because the algorithm is known, the entire sequence can be reproduced if the seed is known.
How Pseudo-Random Number Generators Work
Most programming languages and operating systems rely on PRNGs. A common type is the linear congruential generator (LCG), which uses the formula:
next = (a * current + c) mod m
Here, a, c, and m are constants, and current is the previous value. For instance, with a = 1103515245, c = 12345, and m = 2^31, the sequence repeats after m steps. The seed is the initial current value. If you know the seed and the constants, you can predict every number. Modern PRNGs like the Mersenne Twister (used in Python and many other languages) are far more complex, with a period of 2^19937 – 1, but they are still deterministic. The key point: a PRNG is a mathematical function, not a source of entropy.
Why Deterministic Sequences Are Useful
Determinism is actually a feature in many applications. In video games, a fixed seed allows a game world to be generated identically for every player, or lets a player replay a “random” event by using the same seed. In simulations, researchers need reproducible results to verify experiments. In cryptography, however, determinism is a fatal flaw if the seed is predictable. That’s why cryptographic RNGs (CSPRNGs) are designed to be computationally infeasible to predict, even if the algorithm is known, as long as the seed has enough entropy.
True Random Number Generators (TRNGs)
True randomness comes from physical processes that are fundamentally unpredictable, such as:
- Radioactive decay – the timing of decay events is quantum-random.
- Thermal noise – the random motion of electrons in a resistor.
- Atmospheric noise – captured by radio receivers.
- Quantum effects – such as photon polarization or shot noise.
Hardware RNGs (HRNGs) use sensors to measure these processes. For example, a simple TRNG might measure the jitter in a clock signal or the timing between keystrokes. However, even TRNGs can be biased (e.g., more 1s than 0s), so they often pass the raw data through a cryptographic hash or a conditioner to remove bias. The output is still called “true random” because the underlying source is unpredictable, but the post-processing is deterministic.
Common Problems and Misconceptions
One common misconception is that a PRNG is “broken” if it repeats. All PRNGs eventually repeat; the period is just the length before repetition. A short period (like 2^31 for some LCGs) can be problematic for large simulations, but for most purposes, a period of 2^19937 is effectively infinite.
Another issue is seeding. If a PRNG is seeded with the same value (e.g., the current time in seconds), two runs starting in the same second will produce identical sequences. This has caused real-world bugs, such as online poker sites dealing the same hands twice. A safer approach is to seed from an operating system’s entropy pool, which gathers randomness from hardware events.
Also, many people think that shuffling a list with a PRNG is truly random. But if the PRNG is not cryptographically secure and the seed is known, an attacker can predict the shuffle order. This is why online gambling uses CSPRNGs with seeds from TRNGs.
How to Choose the Right RNG
Choosing an RNG depends on your needs:
- Non-cryptographic simulations, games, or statistical sampling: Use a well-tested PRNG like the Mersenne Twister (in Python,
randommodule) or PCG. These are fast and pass statistical tests. - Cryptography, passwords, tokens, or anything security-related: Use a CSPRNG. In Python, use
secretsmodule; in JavaScript, usecrypto.getRandomValues(); in C, useRAND_bytesfrom OpenSSL. - Scientific research requiring reproducibility: Use a PRNG with a fixed seed and document the seed.
- Lotteries or gambling where unpredictability is critical: Use a hardware TRNG with a CSPRNG for post-processing.
For most developers, the operating system’s built-in randomness (e.g., /dev/urandom on Linux, CryptGenRandom on Windows) is the best choice because it combines a TRNG seed with a CSPRNG.
Real-World Examples of RNG Failures
Several high-profile incidents illustrate the dangers of poor RNGs:
- In 2012, a security researcher found that some Android apps used a weak RNG (Java’s
Randomclass) seeded with the current time, allowing attackers to predict generated Bitcoin private keys and steal funds. - In 2013, the Dual_EC_DRBG standard was suspected of containing a backdoor, leading to its removal from many products.
- In 2016, a vulnerability in a random number generator used by an online gambling site allowed players to predict card shuffles and win consistently.
These cases highlight that even seemingly random numbers can be compromised if the RNG is not properly seeded or is cryptographically weak.
Frequently Asked Questions
Is a random number generator in a calculator truly random?
No. Most calculators use a simple PRNG seeded with the time when you press the button. If you press the button at the exact same millisecond, you get the same number. It’s fine for games, but not for security.
Can a computer generate a truly random number?
Yes, but only with dedicated hardware that measures physical phenomena. Modern CPUs have built-in instructions like Intel’s RDRAND that use thermal noise. Without such hardware, a computer cannot generate true randomness on its own; it relies on external entropy sources.
Why do random numbers in Python look random but repeat after a while?
Python’s random module uses the Mersenne Twister, which has a period of 2^19937 – 1. That’s so long that you’ll never see it repeat in practice. However, if you seed it with the same value, you get the same sequence. The numbers appear random because they pass statistical tests, but they are deterministic.
What is the difference between a seed and a salt?
A seed initializes a PRNG; a salt is a random value added to a password before hashing to prevent rainbow table attacks. Both are random inputs, but they serve different purposes. A seed must be kept secret if you want unpredictability; a salt is not secret but must be unique per password.
Is the random function in Excel truly random?
No. Excel’s RAND() and RANDBETWEEN() use a PRNG that is reseeded each time the worksheet recalculates. The algorithm is not documented and is not suitable for cryptographic purposes. For security, use a dedicated tool or programming language.
How can I test if a random number generator is good?
You can run statistical test suites like NIST SP 800-22 or Dieharder. These tests check for uniformity, independence, and other properties. However, passing these tests does not guarantee unpredictability; a PRNG with a known seed will pass all tests but is still predictable.
Conclusion: Practical Takeaway
The next time you use a “random” function, ask yourself: do I need unpredictability or just statistical randomness? For a game or a simulation, a PRNG is fine. For a password reset token or a cryptographic key, you must use a CSPRNG seeded from a true entropy source. Understanding the difference between pseudo-random and true random is not just academic – it can prevent security vulnerabilities and data loss.
Related
- SUMIF Dropdown in Google Sheets: How to Use
- Wedding Budget Google Sheets: Step-by-Step Guide
- Direct Data Entry Devices: Types and Uses
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Renovation Budget Spreadsheet: Build and Track CostsA renovation budget spreadsheet is a workbook for listing project costs, comparing estimates with actual spending, and seeing…
- Outlook Signature: How to Create One That Looks GoodTo create a nice signature in Outlook, open the signature settings for your version, add a concise block…
- Google Sheets Sum If Cell Contains NumberTo sum cells in Google Sheets only when they contain a number, use SUMIF with a criterion like…
- Excel Conditional Formatting: Apply Rules to a RangeTo apply conditional formatting to a range in Excel for Microsoft 365 on Windows, select the cells, go…
- Schedule an Email in Outlook Webmail: Step-by-StepTo schedule an email in Outlook webmail, start a new message, open the arrow beside Send, choose Schedule…
- Does Excel Have a Profit and Loss Template? How to Use ItYes. Microsoft Excel for Microsoft 365 includes a template gallery where you can search for profit and loss…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.