CalcTrace

Random Number Generator

Pick one random number or a whole list from any range, with or without repeats, as whole numbers or decimals. Every possible result is exactly as likely as any other. There are quick buttons to roll a die or flip a coin.

Last updated .

Your range

Order of the results

Both ends of the range can come up. The die and coin buttons use the “How many” box, so you can roll several dice or flip several coins at once.

Result

Press Generate

–

Choose a range and press Generate.

How the numbers are chosen

The generator takes its randomness from the cryptographic random source built into your browser, which is seeded by your device’s operating system. That source hands over 32-bit numbers: whole numbers from 0 to 4,294,967,295, each equally likely. The work of this page is to turn one of those into a number in your range without favouring any result.

n = highest − lowest + 1

limit = floor(2³² ÷ n) × n

result = lowest + (x mod n), using only draws where x < limit

Here n is how many different results are possible, x is the raw 32-bit draw, 2³² is 4,294,967,296 (the number of values x can take), “floor” means round down to a whole number, and “mod” means the remainder after dividing. The limit is the largest multiple of n that fits inside the source. A draw at or above the limit is thrown away and a new one is taken. This is called rejection sampling.

Worked example: why draws are thrown away

The idea is easier to follow with a small source. Suppose the source gave numbers from 0 to 255 (256 values) and you wanted to roll a die, so n = 6.

  1. Find the limit. 256 ÷ 6 = 42 remainder 4, so limit = 42 × 6 = 252.
  2. Reject the leftovers. The four draws 252, 253, 254 and 255 are discarded.
  3. Map what is left. A draw of 200 gives 200 mod 6 = 2, so the die shows 1 + 2 = 3.

Each face is now produced by exactly 42 of the 252 accepted draws. Skip the rejection step and faces 1 to 4 would each have 43 chances while faces 5 and 6 had only 42. That small tilt is called modulo bias, and it gets worse as the range grows. With the real 32-bit source the limit for a die is 4,294,967,292, so only 4 draws in about 4.3 billion are discarded and you will never notice the retry.

With or without repeats

With repeats allowed, each number is drawn independently, like rolling a die again and again. With repeats off, it works like pulling numbered balls from a bag without putting them back, which is what you want for a raffle, lottery-style picks or a random running order. To shuffle a whole list, switch repeats off and ask for as many numbers as the range contains. You cannot draw more unique numbers than exist: 10 different numbers from 1 to 6 is impossible, and the page will say so.

Repeats turn up far more often than people expect.

P(at least one repeat) = 1 − (n ÷ n) × ((n − 1) ÷ n) × … × ((n − k + 1) ÷ n)

In this formula n is the number of possible results and k is how many numbers you draw. Drawing 10 numbers from 1 to 100 gives a 37.2% chance that two of them match. Drawing 23 numbers from 1 to 365 gives 50.7%, the well-known birthday problem. Two dice show the same face 16.7% of the time. Seeing a repeat is not a sign that a generator is broken, and a generator that never repeated would not be random. When repeats are allowed, the result card shows this probability for your own settings.

Decimals

Choosing decimal places splits the range into equal steps. From 0 to 1 with two decimal places there are 101 possible results (0.00, 0.01 and so on up to 1.00), and each is equally likely, including both ends. “No repeats” then means no repeats at that precision.

What it is suitable for

The output is unpredictable and unbiased, which makes it suitable for games, classroom activities, choosing a winner, picking a sample for a survey or audit, and assigning people to groups. Two limits are worth knowing. First, there is no seed, so a sequence cannot be reproduced later. Research that must be repeatable should use a seeded generator in statistical software and record the seed. Second, nothing is recorded or witnessed. A prize draw that has to meet permit or regulatory rules needs a process that others can verify, such as a witnessed draw.