Pseudo-random vs cryptographically secure numbers
Computers are built to be predictable, so producing randomness takes some care. The simplest generators are pseudo-random: a formula takes a starting value called a seed and churns out numbers that look random but repeat exactly if you start from the same seed. That is fine for a video game, and a real weakness anywhere someone could gain by guessing the next number.
This generator uses your browser's cryptographically secure source, crypto.getRandomValues. It is seeded with unpredictable data (entropy) and designed so that earlier results give no useful clue about later ones. The numbers are generated on your own device.
| Feature | Simple pseudo-random | Cryptographically secure |
|---|---|---|
| Starts from | A seed value | Unpredictable entropy |
| Can be replayed | Yes, from the same seed | No |
| Can be guessed from past outputs | Possible for simple formulas | Not in practice |
| Typical uses | Games, simulations, repeatable tests | Passwords, security keys, fair draws |
| Used here | No | Yes |
Inclusive ranges and how many numbers are possible
Both ends of the range are included. Ask for a number from 1 to 10 and you can get 1 or 10, with every whole number in between equally likely. The count of possible results is one more than the gap between the ends, which is the classic off-by-one trap.
| Range | Possible values | Chance of each |
|---|---|---|
| 1 to 6 | 6 | 16.67% |
| 0 to 9 | 10 | 10% |
| 1 to 100 | 100 | 1% |
| −10 to 10 | 21 | 4.76% |
| 1 to 365 | 365 | 0.27% |
Results are whole numbers. For decimals, scale up and divide: for a number from 0 to 10 with two decimal places, generate from 0 to 1000 and divide the result by 100.
With or without duplicates
Turning duplicates off is like drawing numbered balls from a bag and keeping them out. Leaving duplicates on is like dropping each ball back in before the next draw. Repeats then turn up far sooner than most people expect; it's the same effect behind the famous birthday problem, where 23 people are enough for a better than even chance of a shared birthday.
| Numbers drawn | Chance of at least one repeat |
|---|---|
| 5 | 9.7% |
| 10 | 37.2% |
| 15 | 66.9% |
| 20 | 87.0% |
| 30 | 99.2% |
So if you need ten different numbers, keep duplicates off. You also can't ask for more unique numbers than the range holds: there are only five different whole numbers from 1 to 5.
Lottery odds formula
In a draw where you choose k numbers out of n and the order doesn't matter, the number of possible tickets comes from the combinations formula. Every ticket has exactly the same chance, including the one that reads 1, 2, 3, 4, 5, 6. It looks too tidy to win, but the draw doesn't know that.
Example: pick 6 from 49
- (49 × 48 × 47 × 46 × 45 × 44) ÷ (6 × 5 × 4 × 3 × 2 × 1)
- 10,068,347,520 ÷ 720
- = 13,983,816
One ticket has a 1 in 13,983,816 chance of matching all six numbers.
| Format | Possible tickets |
|---|---|
| Pick 6 from 45 | 8,145,060 |
| Pick 5 from 69 | 11,238,513 |
| Pick 6 from 49 | 13,983,816 |
| Pick 6 from 59 | 45,057,474 |
Drawing raffle winners by ticket number
Numbered tickets make a random number generator the simplest raffle drum there is. Set the range from the first ticket to the last one sold, not the last one printed, or you may draw a ticket nobody holds. Each ticket's chance is 1 divided by the number of tickets, so someone holding several tickets simply has several chances.
Example: a school raffle with three prizes
- Tickets 1 to 250 were sold
- Generate 3 numbers from 1 to 250 with duplicates off, so no ticket can win twice, and sorting off, so the order stays random
- Give the top prize to the first number, the second prize to the next, and so on
- A family holding 5 tickets has a 5 ÷ 250 = 2% chance of the top prize
Three different winning tickets, drawn in one go, with the prize order decided in advance.
Everyday uses for random numbers
- Surprise breaks: generate a minute from 0 to 59 and that's when the class, or the office, gets its five-minute stretch.
- Surveys and spot checks: to contact 20 of 400 customers, number the list and generate 20 numbers from 1 to 400 with duplicates off.
- Classrooms: pick a random page, question or seat. For names rather than numbers, the Random Name Picker is quicker.
- Games: any die you don't own, like a d7 or a d30, is just a range from 1 to 7 or 1 to 30. Standard dice live on the Dice Roller.
- Running order: to put a whole list in random order, use the List Shuffler.
If there's a prize involved, say the range and the rules out loud before you press the button, and do it where everyone can see the screen. It's much harder to argue with a draw you watched.