How computers pick random numbers, and what makes a draw fair

Pseudo-random versus cryptographic randomness, the modulo bias that makes some numbers more likely, drawing without repeats, and the real odds in a raffle.

Open the Random number generator Free, in your browser, nothing uploaded.

When a teacher draws a name, a club runs a raffle or a researcher picks a sample, everyone needs to trust that the pick was fair. A computer cannot flip a coin, so it has to make randomness from something, and a careless method can quietly favour some numbers. This article explains how the random number generator makes each pick and how to work out the odds of a draw. For board games, the dice roller rolls real dice shapes; to split a class into random teams, use the group maker; and to see a picked number in binary or hex, try the number base converter.

What it is

Pseudo-random generators. Most programming languages have a quick built-in generator that produces a long sequence of numbers from a starting value, the seed. The numbers look random, but anyone who knows the method and the seed can predict them.

Cryptographic generators. Browsers also offer crypto.getRandomValues, which mixes in unpredictable events from the device (timing jitter, hardware noise) and is built so that past outputs do not reveal future ones. The tool uses it for every pick.

Modulo bias. The generator gives a whole number from 0 to 2³² − 1 (4,294,967,295). To get a number from 1 to 6, the obvious step is to take the remainder after dividing by 6. But 2³² is not a multiple of 6: it leaves a remainder of 4, so the values 0 to 3 come up very slightly more often than 4 and 5. With a one-byte generator (0 to 255) the effect is easy to see: 256 ÷ 6 leaves 4, so four of the six faces would have 43 chances in 256 and two would have 42.

Rejection sampling fixes it. Any raw number in that uneven leftover strip at the top is thrown away and drawn again. What remains divides evenly, so every value has exactly the same chance.

Drawing without repeats works like taking slips from a hat: once a value is picked, it is removed. The tool does a partial shuffle (the Fisher-Yates method), swapping each picked value out of the pool so it cannot come up again.

Decimals are whole numbers in disguise. Between 0.50 and 2.50 with 2 decimal places there are 201 possible values (50 to 250 hundredths), and each is equally likely.

How to use it

  1. On the Numbers tab, type the Lowest and Highest values.
  2. Set How many and Decimal places (0 for whole numbers).
  3. Tick No repeats for a draw where each value can appear once, and Sort smallest first for a tidy list.
  4. Read the picks and the line with the sum, smallest, largest and average. Press Pick again for a fresh draw.
  5. For names or items, open From my list, type one per line, then set How many to pick or tick Shuffle the whole list.
  6. Press Copy result or Download CSV to keep the record.

A worked example

A school fair sells raffle tickets numbered 1 to 250 and has 3 prizes. Each ticket can win only once, so the draw uses Lowest 1, Highest 250, How many 3, with No repeats ticked. One draw gave 113, 239 and 80; yours will differ.

  • Chance that any one ticket wins a prize: 3 ÷ 250 = 1.2%.
  • Chance that someone holding 3 tickets wins at least once: the chance that none of the three is drawn is (247 × 246 × 245) ÷ (250 × 249 × 248) ≈ 0.9643, so at least one win is about 3.57%, a little under 3 × 1.2%, which would double-count the chance of winning twice.
  • Possible sets of winners: 250 × 249 × 248 ÷ (3 × 2 × 1) = 2,573,000.

Without rejection sampling, how unfair would a 1 to 250 draw be? 2³² ÷ 250 leaves a remainder of 46, so 46 tickets would have one extra chance in about 17 million. That is tiny, but a fair draw should have no tilt at all, and for small generators the tilt is much larger.

Checked with the tools

We ran the tool’s own below() function, read straight from its page code, in Node with the same crypto.getRandomValues. Rolling a die 600,000 times gave between 99,828 and 100,180 for each face; a chi-squared test (a standard check of how far counts stray from equal) gave 1.09, well within chance for 5 degrees of freedom. Drawing 3 of 10 without repeats 100,000 times picked each value between 29,666 and 30,202 times, against 30,000 expected. The remainders 4 (for 6) and 46 (for 250) and the 201 decimal values were worked out the same way.

Where it is used

  • Classrooms. Choosing who answers next without favourites.
  • Raffles and giveaways. A recorded, repeatable method (with the CSV kept) is easy to show to anyone who asks.
  • Research and auditing. Picking a random sample of records, households or invoices to check.
  • Games. Turn order, random events or a dice substitute.

Common mistakes

  • Allowing repeats in a prize draw. Without No repeats, the same ticket can win twice. Tick it whenever each item can appear only once.
  • Asking for more numbers than the range holds. 11 different numbers from 1 to 10 is impossible; widen the range.
  • Redrawing until you like the result. Decide the rules first, and keep the first draw.
  • Expecting randomness to look even. Short runs cluster; three numbers in the same ten are normal.

Questions people ask

Can a random draw be checked later? Not by rerunning it, because the secure generator never repeats. Keep the copied or downloaded result, and draw in front of witnesses if it matters.

Is a shuffled list the same as picking without repeats? Yes. Taking the first N names of a fair shuffle is the same as drawing N names one at a time from a hat.

Is this suitable for legal lotteries? Licensed lotteries have their own certified equipment and rules. Use this for classrooms, games and informal draws.

Sources

  • Numbers come from the browser's secure random generator (crypto.getRandomValues). Rejection sampling removes the slight bias that simple remainders cause, so every value is equally likely.

If you are an author or publisher and want a credit or a passage changed or removed, tell us and we will act quickly.

Tools in this article

Read next