How Random Number Generators Work, and When Random Is Fair Enough
A random number generator picks a value in a range with every value equally likely. That sounds simple, but it raises good questions: how does a computer produce randomness, is it fair, and when is it not good enough?
Open the Random Number Generator
How a random integer in a range is produced
Computers generate a random decimal between 0 and 1, then scale it. For integers from min to max inclusive, the usual formula is floor(random × (max − min + 1)) + min. For a six-sided die (min 1, max 6) that gives a whole number from 1 to 6, each with probability 1/6. The "+ 1" matters: without it, the maximum would never occur.
Pseudo-random versus true random
Most software generators are pseudo-random: a mathematical algorithm that produces a long sequence which looks random and passes statistical tests, but which is determined by an internal starting state called the seed. This is excellent for games, drawings among friends, sampling and simulations. True random numbers come from physical processes such as atmospheric noise or hardware sources. Operating systems also provide cryptographically secure generators that mix such hardware entropy, which is what should be used for security.
Everyday uses
- Picking a winner for a giveaway among numbered entries.
- Rolling dice for tabletop games.
- Choosing a random sample or assigning people to groups.
- Generating test data and examples.
- Settling friendly decisions.
Worked example: how likely is a six?
The chance of at least one six in four rolls of a die is 1 − (5/6)4 = 1 − 0.4823 = 51.8%, which is slightly better than a coin flip. Randomness does not "even out" in the short term: after five heads in a row, the next flip is still 50:50. Expecting a reversal is the gambler's fallacy.
Picking from a list
To choose a random item from a list of N entries, number them 1 to N and generate a random integer in that range. For several distinct picks, such as drawing winners, repeat and discard duplicates until you have enough, which is equivalent to drawing without replacement.
Fairness
Every integer in the range should be equally likely. The scaling formula gives this to a very good approximation for ordinary ranges. You will still see streaks and repeated numbers, which is normal: genuinely random sequences contain more clusters than people expect.
When not to use it
- Passwords, tokens, encryption keys and anything security related: use a cryptographically secure generator or a password manager.
- Regulated lotteries, gambling or contests that require auditable randomness: use a certified process.
- Situations where you must prove the draw was fair to others: use a documented, verifiable method, such as a publicly witnessed draw.
Common mistakes
- Entering a minimum larger than the maximum.
- Forgetting the range is inclusive on both ends.
- Using a single run to judge whether the generator is "really random".
- Treating a pseudo-random sequence as unpredictable for security.
Frequently asked questions
Is the result truly random?
It is pseudo-random, which is suitable for everyday purposes but not for cryptography.
Can the same number come up twice?
Yes. Each draw is independent, so repeats are possible and expected, especially in small ranges.
How do I pick a random decimal?
Generate an integer in a scaled range and divide. For two decimals between 0 and 10, pick an integer from 0 to 1000 and divide by 100.