Computer generated random numbers are produced by algorithms (pseudo-random number generators, or PRNGs) or by physical hardware that measures unpredictable phenomena (hardware random number generators, or HRNGs). Most software uses PRNGs, which are deterministic but statistically mimic true randomness, while HRNGs are used when security demands unpredictability.
What Does “Random” Mean for a Computer?
True randomness is the absence of any pattern or predictability. For a computer, which follows exact instructions, generating true randomness is a challenge. A PRNG starts with a seed value and applies a mathematical formula to produce a sequence of numbers. Given the same seed, the sequence is identical every time. This is useful for debugging or simulations, but it means the numbers are not truly random—they are just spread out in a way that looks random.
Hardware random number generators solve this by using physical processes, such as electronic noise or radioactive decay, to generate numbers. These processes are fundamentally unpredictable, so the numbers are truly random. Many modern CPUs include a hardware RNG (e.g., Intel’s RDSEED instruction) that can be used for cryptographic purposes.
How Pseudo-Random Number Generators Work
Most programming languages and spreadsheet applications rely on PRNGs. A common example is the linear congruential generator (LCG), which uses the formula:
Xn+1 = (a * Xn + c) mod m
Here, a, c, and m are constants, and Xn is the current value. The quality of the sequence depends heavily on these constants. A poorly chosen LCG can have a short period (repeating quickly) or show obvious patterns, which is why modern PRNGs like the Mersenne Twister (used in Python and many other languages) are more complex. They have a period of 219937-1 and pass many statistical tests for randomness.
How to Generate Random Numbers in Practice
If you need random numbers in a program or spreadsheet, you typically call a built-in function. Here are common ways:
- In Excel: Use
=RAND()to get a decimal between 0 and 1, or=RANDBETWEEN(bottom, top)to get an integer in a range. These are volatile functions that recalculate whenever the sheet changes. - In Python: The
randommodule offersrandom.random(),random.randint(a, b), andrandom.choice(sequence). For security, usesecretsoros.urandom(). - In JavaScript:
Math.random()returns a float between 0 and 1. For cryptographic randomness, usewindow.crypto.getRandomValues().
When you need reproducibility (e.g., for a simulation), you can set a seed. In Excel, you can use =RAND() with a seed by using a custom function, but in Python you call random.seed(42) to get the same sequence every run.
Common Problems with Random Number Generators
One major issue is seed predictability. If an attacker knows the seed or the current state of a PRNG, they can predict future numbers. This is why PRNGs are not suitable for cryptography unless they are specifically designed as CSPRNGs (cryptographically secure PRNGs). Another problem is statistical bias—some generators produce numbers that are not uniformly distributed. For example, using rand() % n in C can introduce bias because the range of rand() is not evenly divisible by n.
Hardware RNGs can also fail, though rarely. They may have a bug that makes them predictable, or they may rely on a physical source that is not truly random. That’s why many systems mix hardware entropy with a PRNG using a technique called seeding.
How to Choose the Right Random Number Generator
Your choice depends on the use case:
- Simulations and games: A fast PRNG like the Mersenne Twister is fine. Speed matters more than cryptographic security.
- Cryptography (passwords, tokens, encryption keys): Use a CSPRNG. In Python, use
secrets; in a browser, usecrypto.getRandomValues(). - Statistical sampling: Use a well-tested PRNG like the ones in NumPy or R, which have been validated for uniformity.
- Lotteries or draws: Use a hardware RNG or a CSPRNG to ensure fairness and prevent tampering.
If you are unsure, check the documentation of your language or tool. For example, Excel’s RAND() is not suitable for cryptographic purposes.
Randomness in Security and Cryptography
In security, random numbers are critical for generating encryption keys, nonces, and salts. If an attacker can predict these values, they can break the security. That’s why operating systems maintain an entropy pool—a collection of random data gathered from hardware interrupts, mouse movements, and other sources. When a program asks for random numbers, the OS mixes this entropy with a CSPRNG to produce unpredictable output.
A common misconception is that a PRNG seeded with the current time is secure. It is not—the time is often predictable to within milliseconds, and an attacker can brute-force possible seeds. Always use a dedicated secure random function.
Alternatives to Software Random Number Generators
If you need true randomness in a physical context, you can use:
- Hardware RNGs: Devices like the OneRNG or the ChaosKey plug into a computer and generate random bits from electronic noise.
- Atmospheric noise: Services like random.org use radio noise to generate random numbers, which can be pulled via API.
- Quantum random number generators: These use quantum effects like photon detection to produce randomness, and are used in high-security applications.
For most everyday programming, a good PRNG is sufficient. But for anything involving security, use a CSPRNG or a hardware source.
Frequently Asked Questions
Can a computer generate truly random numbers?
Yes, but only with dedicated hardware that measures physical processes. Without such hardware, a computer uses algorithms that produce pseudo-random numbers, which are deterministic but appear random.
Why do random numbers in Excel change every time I recalculate?
Excel’s RAND() function is volatile, meaning it recalculates whenever the worksheet recalculates. To keep a random value static, copy the cell and paste it as values (Paste Special > Values).
What is the difference between RAND and RANDBETWEEN in Excel?
RAND() returns a decimal between 0 and 1 (inclusive of 0, exclusive of 1). RANDBETWEEN(bottom, top) returns an integer between the two specified numbers, inclusive. For example, =RANDBETWEEN(1,6) simulates a die roll.
Is Math.random() in JavaScript secure for passwords?
No. Math.random() is a PRNG and is not cryptographically secure. For passwords or tokens, use window.crypto.getRandomValues() or the Web Crypto API.
How do I set a seed in Excel to get the same random numbers?
Excel does not have a built-in seed function for RAND(). You can write a custom VBA function that uses a seed, or use the Analysis ToolPak add-in’s RANDBETWEEN with a fixed seed via VBA. Alternatively, use a different tool like Python or R for reproducible random number generation.
Related
- What Is a Data Entry Analyst? Role, Skills, Pay
- Standing Balance Scale Grades Explained
- Random Name Generator: How It Works and How to Use One
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- How to Double Space Bullet Points in Microsoft WordTo double-space bullet points in Microsoft Word, select the bullet paragraphs, then choose Home > Line and Paragraph…
- Excel Not Responding: Should You Wait or Force Quit?If Excel says Not Responding, wait briefly if the workbook may be calculating, refreshing data, or saving; do…
- Excel Tips and Tricks Cheat Sheet for Microsoft 365For a fast Excel cheat sheet, turn a data range into a table with Ctrl+T, use Ctrl+Shift+L to…
- Excel Data Validation Failed? Common Causes and FixesIn Excel, a data validation failure usually means an entered value does not meet the rule set for…
- How to Insert a Small Checkbox in WordTo insert a small checkbox in Word, add the ballot-box symbol ☐ from Insert > Symbol > More…
- Notepad++ Text to Columns: Split, Align, and Edit DataNotepad++ does not have a one-click, Excel-style Text to Columns command. For simple delimited text, you can replace…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.