How Random Number Generators Work
Computers follow instructions, so where does randomness come from? Learn the difference between pseudo-random and secure generators, the hidden bias that breaks many simple pickers, and how fair random draws are made.
Picking a raffle winner, shuffling a quiz, choosing a random sample for a survey or settling who goes first: random numbers show up in everyday tasks all the time. A computer, though, is a machine that does exactly what it is told. Producing numbers that nobody can predict, and making sure every number is equally likely, takes more care than it first seems.
Pseudo-random generators
Most programming languages ship with a pseudo-random number generator. It starts from a value called the seed and applies a fixed formula over and over to produce a long stream of numbers that look random. They pass many statistical tests, and they are fast.
But the stream is completely determined by the seed. Run the generator again with the same seed and you get exactly the same "random" numbers. That is useful in science and games, where you may want to repeat an experiment. It is a problem whenever someone could gain by predicting the next result, because a clever observer who sees enough output can sometimes work out the internal state.
Secure random generators
A cryptographically secure random number generator is designed so that, even after seeing many outputs, nobody can predict the next one in practice. Operating systems feed it with unpredictable data collected from the hardware, such as tiny timing differences, and mix that in with strong cryptographic functions. The same kind of generator creates encryption keys and secure passwords.
Modern browsers expose such a generator to web pages. The Random Number Generator on this site uses it, and the numbers are created on your own device, so nothing is sent to a server.
The hidden bias: turning raw bits into a range
A secure generator produces raw values, typically whole numbers from 0 up to some power of two. You almost never want that range. You want 1 to 6 for a die, or 1 to 100 for a prize draw. The obvious way to squeeze a raw value into your range is to take the remainder after division, called the modulo. This quietly breaks fairness.
Here is a small example. Suppose the source gives 256 equally likely values, 0 to 255, and you want a number from 0 to 99 by taking the remainder after dividing by 100.
- Raw values 0, 100 and 200 all map to 0. The same is true for every result from 0 to 55: each has three raw values behind it.
- Results 56 to 99 have only two raw values each, because there is no 256 to 299.
So each of the numbers 0 to 55 comes up with a chance of 3 in 256, about 1.17%, while each of the numbers 56 to 99 has 2 in 256, about 0.78%. The low numbers are 50% more likely than the high ones. In a fair draw, every number would have exactly 1%.

This is called modulo bias. It appears whenever the size of your range does not divide evenly into the number of raw values.
Fixing it: rejection sampling
The cure is simple. Find the largest multiple of your range size that fits in the raw range, and throw away any raw value at or above it. In the example, the largest multiple of 100 below 256 is 200. Raw values 200 to 255 are rejected and a new value is drawn. Every result from 0 to 99 is then backed by exactly two raw values, so all are equally likely.
With a small 8-bit source, about 22% of draws would be rejected, which is wasteful. Real generators use much larger raw ranges. With 32-bit values there are 4,294,967,296 possibilities, and for a range of 100 only the top 96 of them are thrown away. Rejection almost never happens, but when it does, it keeps the result perfectly fair. This is what the Random Number Generator does for every number it produces.
Duplicates and the birthday surprise
Should the same number be allowed twice? It depends on what you are modeling.
- With duplicates: rolling dice, simulating repeated trials or choosing a random number each day. Every draw is independent.
- Without duplicates: drawing raffle tickets, picking a lottery line or selecting people for a sample.
People are often surprised by how quickly repeats appear when duplicates are allowed. Draw just 10 numbers from 1 to 100 and the chance that at least two of them match is about 37%. This is the same effect behind the famous birthday problem, where 23 people give better than even odds of a shared birthday. If repeats would ruin your result, turn duplicates off rather than hoping they will not happen.
How to use the random number generator
Enter the minimum and maximum (both are included), choose how many numbers you need, and decide whether duplicates are allowed. Tick the sort option if you want the list in order, for example for lottery numbers, and use Copy to paste the results elsewhere. Each press of Generate is a fresh draw that knows nothing about the previous one.
Good to know
- Random does not mean evenly spread. Short runs of random numbers often contain clusters and gaps. Four heads in a row is normal, not a sign of a broken coin.
- Past results do not change future odds. A number that has not come up for a while is not "due".
- Odds can be tiny. There are 13,983,816 different ways to choose 6 numbers from 1 to 49, and every one of them is equally likely, including 1, 2, 3, 4, 5, 6.
- Official draws have their own rules. For prize draws, regulated games or security uses, follow the method required by the organizer or the rules that apply.
FAQ
Can a computer generate truly random numbers?
Hardware can measure physical noise that is genuinely unpredictable, and operating systems use such sources to seed their secure generators. The numbers you receive are produced by a cryptographic algorithm from that seed, which for practical purposes is just as unpredictable.
Why do I keep getting the same number twice?
If duplicates are allowed, repeats are expected, and they come sooner than intuition suggests. Turn duplicates off if every number must be different.
Is it fair to pick the winner of a contest this way?
A secure, unbiased generator gives every entry the same chance, which is what fairness requires. For contests with legal requirements, check the rules that apply and keep a record of how the draw was made.
How do I pick random items from a list instead of numbers?
Number the items from 1 upward, generate numbers in that range without duplicates, and take the items with those numbers. This works for names, questions, songs or anything else you can put in a list.