How Random Number Generators Work: True vs Pseudo-Random
A computer is a machine that follows instructions exactly. So how can it flip a coin, roll a die or pick a random name? The short answer: most of the time it does not make truly random numbers at all. It makes numbers that only look random, and for most uses that is enough. Here is how random number generators work, how they can go wrong, and how the tools on this site pick their results.
Two Kinds of Random
- True random numbers come from something physical that nobody can predict: electrical noise in a circuit, radioactive decay, radio static from the atmosphere. Random.org, for example, uses atmospheric noise.
- Pseudo-random numbers come from a formula. You start with a number called the seed, run it through the formula, and get the next number, then the next. The result looks random, but the same seed always gives the same sequence.
Pseudo-random sounds like cheating, but it is what nearly every game, app and simulation uses. It is fast, and a good formula passes the statistical tests that true randomness passes.
The First Computer Method, and Its Flaw
Around 1949 the mathematician John von Neumann suggested the middle-square method: square a number, take the middle digits, and repeat. Start with 1234: its square is 01522756, so the next number is 5227, then 3215, 3362, 3030 and so on.
It looks random, but it falls into loops. Start with 2916 and you get 5030, 3009, 0540, 2916, and then the same four numbers forever. Von Neumann knew this. He used the method because it was fast, and he checked the output for problems. His famous joke was that anyone who tries to make random numbers by arithmetic is "in a state of sin".
Before Computers: Books of Random Numbers
Scientists used to need random numbers so badly that they were printed in books. In 1955 the RAND Corporation published A Million Random Digits with 100,000 Normal Deviates, made with an electronic roulette wheel. Researchers picked a random page and read off digits. Long before that, people used dice, coins and drawn lots, as you can read in our article on dice in the ancient world.
How Modern Generators Work
Today's pseudo-random generators are much better. Common ones include:
- Mersenne Twister: the default in Python and many other tools. It only repeats after 219937 − 1 numbers, a number with about 6,000 digits.
- xorshift128+: the generator behind Math.random() in Chrome and other browsers built on the V8 engine. Very fast, good for games and animations.
- Cryptographic generators: the kind used for passwords and encryption keys. Your operating system mixes in unpredictable physical events, such as tiny timing differences in hardware, so even someone who knows the formula cannot predict the next number.
Ordinary generators are fine for a game. But if someone sees enough of their output, they can sometimes work out the rest. For anything where fairness or security matters, the cryptographic kind is the right choice.
A Hidden Trap: Modulo Bias
Even a perfect generator can give unfair results if it is used carelessly. Say you have a random byte, a number from 0 to 255, and you want a die roll. The easy way is to divide by 6 and use the remainder. But 256 does not divide evenly by 6, so four of the faces get one extra chance:
| Die face from one random byte (0 to 255), using the remainder after dividing by 6 | Chance |
|---|---|
| 1, 2, 3 or 4 | 43 in 256 each (16.8%) |
| 5 or 6 | 42 in 256 each (16.4%) |
That is a small difference, but over thousands of rolls it shows. The fix is simple: if the random number lands in the uneven leftover part, throw it away and draw again. This is called rejection sampling.
How Our Tools Pick
Our Coin Flip, Dice Roller, Random Number Generator, Random Name Picker and the other tools that share the same code use your browser's cryptographic generator, called crypto.getRandomValues. They also use rejection sampling, so every face, number and name has exactly the same chance. Older browsers without that feature fall back to Math.random(). Everything runs in your own browser; nothing is decided on our server.
Is It Really Random, Then?
For a coin flip or a dice roll, a good generator is better than the real thing. Real coins have a tiny bias toward the side they start on, as explained in our article on coin flip odds, and real dice can be slightly uneven. A good generator has neither problem.
The bigger risk is the person reading the results. We see patterns in streaks and expect the "missing" result to be due. That is the gambler’s fallacy, and it fools people much more often than any computer fools them. To see how bad people are at making up random numbers themselves, read whether are humans random.
FAQ
Can a computer generate truly random numbers?
Not with a formula alone. Formulas give pseudo-random numbers that only look random. For true randomness, computers measure unpredictable physical events like electrical noise, and many devices have hardware for this.
What is a seed in a random number generator?
The starting number for the formula. The same seed always gives the same sequence, which is useful for testing and for games that let you share a world or level by its seed.
Is Math.random() truly random?
No. It is a fast pseudo-random generator, fine for games but not for anything secret or where money is at stake. For that, browsers offer crypto.getRandomValues.
Are online coin flips and dice fair?
A well-made one is. Our coin flip and dice roller use the browser's cryptographic generator with rejection sampling, so every result has exactly the same chance.
What is modulo bias?
A small unfairness that appears when you squeeze a random number into a smaller range using the remainder, and the range does not divide evenly. Rejection sampling removes it.