How Random Numbers Are Made, and How to Pick One Fairly
How a random number generator turns random bits into a fair pick from a range, why a simple remainder can be biased, and when to use unique numbers.
In this article
Picking a random number sounds simple: choose a lowest and a highest value and let chance decide. Behind the scenes, though, there are two separate jobs. The first is to get a supply of unpredictable random bits. The second is to turn those bits into a number inside your range so that every value has exactly the same chance. Getting the second step wrong is a common source of subtle unfairness. This guide explains both jobs, with examples that you can try in the Random Number Generator on this site, which shows its working under the result.
Where the randomness comes from
Computers are built to be predictable, so most random numbers are produced by an algorithm called a pseudorandom number generator. It starts from a seed value and produces a long sequence that looks random. Wikipedia explains that such generators are fast and repeatable, which is useful for simulations and testing, but their output can be predicted by anyone who learns the seed or the internal state. For a game or a classroom draw that does not matter. For anything where guessing the next value would cause harm, a cryptographically secure source is used instead.
Web browsers provide such a source through the Web Crypto interface. MDN Web Docs describes the getRandomValues() method as filling an array with cryptographically strong random values, using a generator seeded with enough entropy. The Random Number Generator on this site uses that method rather than the simpler Math.random function, and everything happens in your browser, so no numbers are sent anywhere.
From random bits to a number in your range
Suppose you want a whole number from 1 to 100. There are 100 possible values, so the generator needs a number from 0 to 99 and then adds the lowest value, which is 1. The general formula is: number = lowest + a random whole number from 0 to (highest − lowest). A range of 1 to 100 gives 100 − 1 + 1 = 100 values, so the offset runs from 0 to 99. If the offset is 37, the result is 1 + 37 = 38.
The obvious way to get an offset from a large random value is to take the remainder after dividing by the number of possible values. That is where the bias hides. Imagine the generator produced only the values 0 to 9 and you wanted a number from 0 to 5, six values. Taking the remainder after dividing by 6 maps 0 and 6 to 0, 1 and 7 to 1, 2 and 8 to 2, 3 and 9 to 3, but only 4 and 5 once each. The values 0 to 3 are now twice as likely as 4 and 5. With 53 random bits the effect is much smaller, but it is still present unless it is dealt with.
Rejection sampling removes the bias
The fix is called rejection sampling. Work out the largest multiple of the number of values that fits inside the supply of random numbers, and throw away any draw at or above it, drawing again. With 53 random bits there are 2 to the power 53, or 9,007,199,254,740,992, possible draws. For six values the largest multiple of 6 that fits is 9,007,199,254,740,990, so only the last two draws are discarded and every remaining draw maps evenly onto the six values. The generator on this site does exactly this, which is why every value in your range has the same chance.
Unique numbers and repeats
When repeats are allowed, every draw is independent, so the same number can appear twice in a row, just as a die can land on six twice. When repeats are not allowed, such as when drawing raffle tickets, a number that has already come up is skipped. If you ask for nearly the whole range, for example 10 different numbers from 1 to 10, the generator instead shuffles the full list and takes the first ones, which is quicker than repeatedly rejecting numbers already used. Asking for more unique numbers than the range holds is impossible, and the tool tells you so.
- Whole numbers from 1 to 6 imitate a die; 1 to 100 suits a quick draw or a game
- Use the unique option for raffles, lotteries and picking students without repeats
- Decimals are made by picking a whole number in steps and dividing, for example 0.00 to 1.00 in steps of 0.01
- A draw of 1,000 numbers is the largest the tool makes at once
What a random pick is not
A fair generator does not avoid streaks. Getting the same number twice, or three odd numbers in a row, is normal and does not mean the generator is broken. Each draw has no memory of the one before. Also, the generator on this page is meant for games, samples and everyday decisions. It is not a password tool, because passwords have extra rules about length and character sets. Use a dedicated password generator for that.
Sources
Share
Keep reading
Related articles
How a Scientific Calculator Works Out an Expression
How the order of operations, brackets, powers, functions and degrees or radians decide the answer to a typed expression, with examples to check.
Standard Deviation Explained: Sample or Population
What standard deviation measures, how to work it out step by step, why a sample divides by n minus 1, and a worked example you can check.
How to Solve a Triangle: Sides, Angles and Area
How to find every side, angle, the perimeter and the area of a triangle from three sides, two sides and the angle between them, or two angles and a side.