How Random Number Generators Work
6 min read
When you spin a wheel and it lands on a winner, something has to decide that result. On a computer, that decision comes from a random number generator, and the way it is implemented decides whether the draw is genuinely fair.
Computers are not naturally random
A computer does what an instruction tells it, so it cannot produce randomness from nothing. Instead it relies on a pseudo-random generator: an algorithm that takes a starting value (a seed), runs it through a maths function, and produces a long stream of numbers that look random.
Modern browsers seed that generator from entropy the operating system collects — timing jitter, hardware events, and device noise. Because the seed is not predictable, the output is not predictable either, even though the algorithm itself is public and deterministic.
Pseudo-random is not all the same
This is where a lot of online pickers fall short. There are two very different functions in JavaScript:
Math.random()is fast, cheap, and explicitly documented as not suitable where results must be unpredictable. Two calls in the same millisecond can be correlated.crypto.getRandomValues()comes from the Web Crypto API and is designed for cryptography. Output is not predictable, and the entropy is taken from the operating system.
PickTwirl uses the second one. The MDN reference for Crypto.getRandomValues() explains the difference in full, and the same page links to the NIST recommendation that defines how such generators are built.
The subtle bias almost everyone misses
Getting a secure random number is only half the job. You also have to turn it into an index in a list without favouring any position.
The naive version looks like this:
index = randomNumber % numberOfEntries
That is biased. A random number covers a fixed range, and if the number of entries does not divide that range evenly, the first few entries get more of the remainder. With six entries and a 32-bit random value, the difference is tiny, but tiny is not the same as zero.
The fix is rejection sampling: pick a random number, and if it lands in the uneven tail at the top of the range, throw it away and pick again. Every index then receives exactly the same number of possible random values. It costs an extra loop now and then, and it makes the result exactly fair.
Why "it landed on the same option twice" is normal
People expect randomness to look mixed. It does not, at small sample sizes. Ten spins of a six-option wheel will usually show a repeat, and that is not evidence of a broken picker. If you want to see the distribution settle down, run a hundred spins and count.
This is also why "random" draws sometimes need the winner removed. For a raffle with several prizes, you do not want the same entry to win twice, so you draw without replacement instead of relying on chance.
What to check in any random picker
- Does it say which random source it uses? "Cryptographically secure" or "the browser's crypto API" is a good sign. Silence, or "Math.random", is not.
- Does the winner appear before the animation, or does the animation decide? A fair tool decides first and animates to the result, so the spin cannot influence the outcome.
- Is the draw local? If selection happens on a server, you are trusting that server.
- Does it let you remove duplicates, so one entry does not quietly get more segments?
PickTwirl does all four, and every claim is checkable against the public sources we link to.
More guides
Sources & further reading
Official references on random number generation, sampling, and teaching with these tools.
See sources & learning resources