← Blog

How Random Number Generators Work, and How to Tell If One Is Fair

When you press "Generate" on a random number generator, a computer, which is designed to follow instructions precisely, produces something nobody can predict. This guide explains how that works, the most common way a generator can be subtly unfair, and how to judge whether results are random.

Three kinds of randomness

Pseudo-random number generators

A pseudo-random number generator (PRNG) is a formula. It starts from a value called a seed and repeatedly transforms it to produce a sequence that looks random. Given the same seed, it produces exactly the same sequence again. That is useful for simulations and video games that need repeatable results.

JavaScript's Math.random() is a PRNG. It is fine for animations and casual games. However, browsers do not guarantee that its output is unpredictable, and the specification does not require any particular quality. It should not be used for passwords, prize draws, or anything where someone could benefit from guessing the next result.

Cryptographically secure generators

A cryptographically secure PRNG (CSPRNG) is designed so that seeing previous outputs does not help predict the next one. Operating systems seed it from unpredictable physical events, such as hardware timing noise, and keep mixing in new entropy. In browsers, it is available through the Web Crypto API as crypto.getRandomValues().

Hardware random number generators

A hardware generator measures a physical process, such as electrical noise, and converts it into bits. Modern processors include one, and operating systems combine its output with other sources to seed their CSPRNG. Most people never need to access it directly.

What Randomify uses: every result is drawn from crypto.getRandomValues() in your own browser, which all current browsers support. Numbers are generated on your device, and lists you enter are not uploaded to generate a result.

The hidden unfairness: modulo bias

A random source supplies bits, not "a number from 1 to 6". Converting one into the other is where many generators go wrong.

Suppose a source produces a random byte, a whole number from 0 to 255, and a program needs a die roll. The shortcut is to take the remainder after dividing by 6, then add 1. But 256 is not a multiple of 6: 256 = 42 × 6 + 4. Remainders 0, 1, 2, and 3 can each be produced in 43 ways, while 4 and 5 can be produced in only 42. Faces 1 to 4 are slightly more likely than 5 and 6.

The difference is small, 43/256 instead of 42/256, but it is a systematic bias rather than random variation. With a larger range relative to the source, the bias becomes larger.

The fix is rejection sampling: discard any raw value from the incomplete final block and draw again. In the byte example, only values 0 to 251 are accepted, so each face has exactly 42 ways of occurring. Randomify uses 32-bit values with rejection sampling, so every number in your range is equally likely.

Try it with any range:

Try it: Random Number Generator
Loading tool…

Shuffling fairly

Picking names from a list or splitting people into teams requires a fair shuffle: every possible order should be equally likely.

The standard method is the Fisher–Yates shuffle. Starting from the last item, swap it with an item chosen uniformly at random from those not yet fixed, including itself, then move one position left. With n items, it produces each of the n! possible orders with equal probability.

A popular shortcut is to sort a list by random comparisons. It does not produce equally likely orders, and the bias depends on the sorting algorithm. Randomify's name picker and team generator use Fisher–Yates.

Random results look less random than people expect

People tend to expect random sequences to alternate more than they do. A real random sequence contains streaks, clusters, and repeats.

Fair coin flipsProbability of at least one run of…Chance
103 identical results in a row82.6%
104 identical results in a row46.5%
1005 identical results in a row97.2%
1006 identical results in a row80.7%
1007 identical results in a row54.2%

So a run of six heads in a hundred flips is not evidence of a problem; its absence would be more surprising. The same applies to a random number generator returning the same number twice in a row. With a range of 1 to 10, that will happen on about one draw in ten.

Try it yourself: flip 100 coins at once and look for the longest streak.

How to check whether a generator is fair

You cannot prove a generator is fair from a few results, but you can check for obvious problems:

  1. Collect many results. Small samples vary a lot. For a 1–6 generator, collect hundreds or thousands of draws, not twenty.
  2. Compare frequencies with expectations. Each value should appear about equally often. Statisticians use a chi-square test to measure whether the differences are larger than chance would usually produce.
  3. Look at sequences, not only counts. A generator could produce perfect frequencies by cycling 1, 2, 3, 4, 5, 6. Check repeats and runs too.
  4. Ask how it works. A trustworthy tool should say which random source it uses and how it maps it to a range.

For high-stakes uses, such as security keys, legally regulated draws, or scientific sampling, use a documented generator that meets the relevant standard and keep a record of the process.

Frequently asked questions

Is a computer's random number truly random? A CSPRNG is deterministic once seeded, but it is seeded from physical unpredictability, and its output cannot practically be predicted without the internal state. For everyday draws, games, and passwords, it is the right choice.

Can I reproduce a result later? Not with a cryptographic generator, which is intentional. To make a draw verifiable, record it on screen or have witnesses present, as described in how to run a fair online raffle.

Why did I get the same number twice? Because each draw is independent. Turn off duplicates if you need every result to be different, for example when drawing several winners.

Try These Tools