No, most random number generators are not truly random—they are deterministic algorithms that produce sequences that only appear random. True randomness exists only in physical processes (like radioactive decay or atmospheric noise), while the typical generator in a computer, game, or app is a pseudorandom number generator (PRNG) that follows a fixed mathematical formula.
What Exactly Is a Random Number Generator?
A random number generator (RNG) is any system that produces a sequence of numbers that lack any predictable pattern. There are two broad categories:
- True Random Number Generators (TRNGs) – derive randomness from physical phenomena, such as electronic noise, quantum effects, or radioactive decay. These are genuinely unpredictable.
- Pseudorandom Number Generators (PRNGs) – use mathematical algorithms to produce long sequences that mimic randomness. They are deterministic: given the same starting point (seed), they always produce the same sequence.
Most software RNGs are PRNGs because they are fast, reproducible, and require no special hardware.
How Pseudorandom Generators Work
A PRNG starts with a seed—an initial number—and then applies a mathematical transformation repeatedly to generate the next numbers. For example, the linear congruential generator (LCG) uses the formula:
Xn+1 = (a * Xn + c) mod m
where a, c, and m are constants. The sequence looks random but is entirely determined by the seed and the constants. If you know the seed and the algorithm, you can predict every number.
Modern PRNGs like the Mersenne Twister (used in many programming languages) are far more sophisticated, with periods so long (219937–1) that they won’t repeat for practical purposes. However, they are still deterministic.
Why “Random” Isn’t Always Random
The word “random” in everyday language means “unpredictable.” But in computing, “random” often means “statistically indistinguishable from random” for a given purpose. This distinction matters:
- Games and simulations – need unpredictability to feel fair, but a PRNG is fine because the user can’t easily predict it.
- Cryptography – requires true unpredictability. If an attacker can guess the seed or the algorithm, they can break encryption. For this, cryptographically secure PRNGs (CSPRNGs) are used, which are designed to be resistant to prediction even if the algorithm is known.
- Scientific sampling – often uses PRNGs because reproducibility is useful (you can rerun an experiment with the same random numbers).
Common Problems: Bias and Predictability
PRNGs can fail in two major ways:
- Bias – some generators produce numbers that are not uniformly distributed. For example, a simple modulo operation on a limited range can bias results. This is why good libraries use techniques like rejection sampling.
- Predictability – if the seed is weak (e.g., based on time), an attacker can guess it. Many early PRNGs used the system clock as a seed, which is predictable to within milliseconds.
A famous example is the 1994 Netscape browser bug, where the seed was derived from the time of day and the process ID, allowing researchers to predict “random” SSL keys. This led to a security vulnerability.
How to Choose a Random Number Generator
Choosing the right RNG depends on your use case:
- For non-security applications (games, simulations, testing): use a well-tested PRNG like the Mersenne Twister or PCG. These are fast and have good statistical properties.
- For security or cryptography: use a CSPRNG like
CryptGenRandom(Windows),/dev/urandom(Linux), or thesecretsmodule in Python. These draw entropy from the operating system’s random pool. - For true randomness (lotteries, scientific experiments that need physical entropy): use a hardware TRNG or an online service like random.org that uses atmospheric noise.
When using a PRNG, always seed it with a high-entropy source, especially for security purposes. For reproducibility, you can seed with a fixed value.
Comparing PRNGs and TRNGs
Here’s a quick comparison:
- Speed – PRNGs are extremely fast; TRNGs are slower because they rely on physical measurement.
- Reproducibility – PRNGs can be reproduced with the same seed; TRNGs cannot.
- Security – TRNGs are inherently unpredictable; PRNGs are only as secure as their algorithm and seed.
- Cost – PRNGs are free and built into every language; TRNGs may require special hardware or external services.
- Use cases – PRNGs for most software; TRNGs for key generation, gambling, and high-stakes security.
Safety and Security Concerns
Using a weak RNG can have serious consequences. For example, if a password reset token is generated with a predictable PRNG, an attacker could guess it and hijack an account. Similarly, if a lottery or online casino uses a biased RNG, players could exploit it.
To stay safe:
- Never roll your own RNG for security purposes. Use established libraries.
- Always use a CSPRNG for passwords, tokens, encryption keys, and session IDs.
- Be wary of “random” features in older software that may use weak seeds.
- For high-stakes applications, consider using a hardware TRNG or a certified randomness source.
Frequently Asked Questions
Is the random function in Excel truly random?
No. Excel’s RAND() and RANDBETWEEN() use a PRNG with a seed based on the current time. They are fine for simulations and sampling, but not for cryptography.
Can I make a truly random number generator with my computer?
Yes, you can use a hardware random number generator (HRNG) that measures electronic noise, or you can use an online service like random.org that uses atmospheric noise. Most operating systems also provide a CSPRNG that mixes hardware entropy, which is considered secure enough for most purposes.
Why do some people say “random” numbers are not random?
Because most software RNGs are deterministic. They produce sequences that look random but are actually predictable if you know the seed and algorithm. Only physical processes are truly random.
What is a seed in a random number generator?
A seed is the initial value that starts the PRNG algorithm. The same seed always produces the same sequence. For security, the seed must be unpredictable; for reproducibility, you can use a fixed seed.
Are there any examples of random number generators failing?
Yes. In 2012, a vulnerability in the Android SecureRandom class caused Bitcoin wallets to generate the same private keys, leading to theft. Also, many early video games had predictable RNGs that players exploited to get favorable outcomes.
How can I test if a random number generator is good?
You can run statistical tests like the chi-square test, the runs test, or use tools like the National Institute of Standards and Technology (NIST) Statistical Test Suite. For most users, using a well-known library is sufficient.
Related
- Generate Random Number in Golang: A Complete Guide
- How to Unmerge Excel Cells (2 Quick Ways)
- How to Ungroup Cells in Excel (Rows & Columns)
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Excel: Protect a Sheet but Keep Slicers Usable for UsersYes. In Excel for Microsoft 365 on Windows, unlock the slicer before protecting the worksheet, then protect the…
- How to Open Find and Replace in Excel: Shortcuts and StepsIn Excel for Microsoft 365 on Windows, press Ctrl+H to open Find and Replace directly on the Replace…
- How to Use COUNTIF in Google SheetsTo count cells that meet one condition in Google Sheets, use =COUNTIF(B2:B100,"Paid"); this example assumes payment statuses are…
- Conditional Formatting Access in Excel: Where to Find ItIn Excel for Microsoft 365 on Windows, access conditional formatting from Home > Conditional Formatting. Select the cells…
- How to Make Columns the Same Width in Microsoft WordTo make columns the same width in Microsoft Word, open Layout > Columns > More Columns, choose the…
- How to Delete Leading Zeros in Excel Without Losing DataTo delete leading zeros in Excel, first check whether they are part of a text value or only…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.