A random number generator picks numbers with no predictable pattern – or at least, that's how it looks from the outside. Behind the scenes, most computer-generated "random" numbers come from a precise mathematical recipe. Understanding that recipe helps you choose the right tool for a classroom draw, a game, a research sample, or a security password.
This guide explains how these algorithms work, why a computer struggles to be truly random, and how to make sure your results are fair.
What Is a Random Number Generator?
A random number generator (RNG) is any method that produces numbers where each outcome cannot be reliably predicted. There are two main families:
- True random number generators (TRNGs): use unpredictable physical events
- Pseudo-random number generators (PRNGs): use math formulas that appear random
Most websites, apps, and spreadsheets use the second type because it is fast, repeatable, and good enough for everyday tasks.
True Random Numbers: Randomness From Nature
A true random number generator measures a physical process that cannot be predicted, such as:
- Electrical noise in a circuit
- Radioactive decay timing
- Atmospheric noise picked up by a radio receiver
- Tiny timing differences in hardware events
These sources are genuinely unpredictable. The downside is that they are slower and need special hardware. Many modern computer chips include a hardware source used to "seed" software generators.
Pseudo-Random Numbers: Randomness From Math
A pseudo-random random number generator starts with a number called the seed, then applies a formula over and over to produce a long sequence. The same seed always produces the same sequence. This is a feature, not a bug – scientists use it to reproduce simulations exactly.
A Simple Example: The Linear Congruential Generator
One of the oldest designs is the linear congruential generator (LCG):
Next = (a × Current + c) mod m
Let's use tiny numbers so you can follow along: a = 5, c = 3, m = 16, and a seed of 7.
| Step | Calculation | Result |
|---|---|---|
| Seed | – | 7 |
| 1 | (5 × 7 + 3) mod 16 | 6 |
| 2 | (5 × 6 + 3) mod 16 | 1 |
| 3 | (5 × 1 + 3) mod 16 | 8 |
| 4 | (5 × 8 + 3) mod 16 | 11 |
| 5 | (5 × 11 + 3) mod 16 | 10 |
The output – 6, 1, 8, 11, 10 – looks random, but it's fully determined by the seed and the formula. With m = 16, the sequence repeats after at most 16 numbers. Real generators use enormous values so the cycle is practically endless.
Modern Algorithms
- Mersenne Twister: a very popular PRNG with an extremely long cycle; common in programming languages and spreadsheets
- PCG and Xorshift families: fast, high-quality generators used in games and simulations
- Cryptographically secure generators (CSPRNGs): designed so that seeing past outputs doesn't help predict future ones
Which Type Should You Use?
| Task | Good Choice |
|---|---|
| Classroom draws, games, giveaways | Standard PRNG |
| Scientific simulations | High-quality PRNG with a recorded seed |
| Passwords, encryption keys, tokens | Cryptographically secure generator |
| Official lotteries | Certified hardware or audited systems |
Pro tip: Never use an ordinary random number generator for passwords or security codes. Use your operating system's or browser's secure random function, or a trusted password manager.
How to Pick a Number in a Range Fairly
Most tools let you set a minimum and maximum, such as 1 to 100. Behind the scenes, the program converts raw random bits into your range. Done carelessly, this can create bias.
The Modulo Bias Problem
Suppose a program takes a random byte (0–255, so 256 values) and uses "value mod 6" to simulate a die. Because 256 isn't divisible by 6:
- Results 0–3 each appear 43 times out of 256
- Results 4–5 each appear only 42 times
The difference is small, but it's real. Well-built generators fix this by rejecting values that fall in the uneven leftover range and drawing again.
The Unique Value Section: How to Run a Fair Draw People Trust
Fairness isn't only about math – it's about trust. If you use a random number generator for a giveaway, team selection, or prize draw, follow these steps:
- Number your list first. Assign each entrant a number before drawing, and share the list.
- Announce the range and rules in advance, including whether repeats are allowed.
- Use "no repeats" mode when picking multiple winners.
- Record the draw. A screen recording or live stream removes doubts.
- For research, save the seed so others can repeat the selection exactly.
These small habits make results transparent and easy to verify.
Common Myths About Randomness
- "Streaks mean it's broken." Real randomness produces streaks. Getting the same number twice in a row is normal.
- "A number is due." Past results don't change future odds in an independent draw. This is known as the gambler's fallacy.
- "Random means evenly spread." In small samples, results often look clumpy. Even distribution only appears over many draws.
How Generators Are Tested
Quality generators are checked with statistical test suites that look for hidden patterns, such as:
- Whether each value appears about equally often
- Whether pairs or sequences repeat too often
- Whether numbers are correlated with earlier outputs
A generator that passes these tests is considered suitable for general use, though security uses need stricter standards.
Expert Tip and Final Takeaway
A random number generator is either measuring unpredictable physical events or running a clever formula from a seed. For everyday draws and games, a standard pseudo-random tool is perfectly fine. For security, always use a cryptographically secure source.
Expert tip: If you need results that others can check later – for a study, a contest, or a class activity – write down the tool, the settings, and the seed if available. A random result that can be verified is far more trustworthy than one that can't.