Skip to the calculator
Rule Calculator

Random number generator

Whole numbers from a range you choose, with or without repeats. Drawn from your browser's cryptographic generator and mapped into the range by rejection sampling, so no outcome is likelier than any other.

By Alex Seote, Built and maintains Rule Calculator

Method checked against the Web Crypto specification · How we check

Where these come from

Every number above comes from your browser's cryptographic generator, crypto.getRandomValues, and is mapped into your range by rejection sampling: draws that would land in the uneven tail are thrown away and taken again. Nothing is sent anywhere — the draw happens on this page, on your machine.

The two ways a generator goes wrong

The first is the source. A pseudo-random generator produces a sequence that looks random and is entirely determined by a hidden state, and for the ones built into browsers that state can be recovered from a modest run of outputs. Nothing about the numbers looks different; they are simply predictable to anyone who cares to.

The second is the mapping, and it survives even a perfect source. Reducing a random value modulo the size of your range spreads the leftovers unevenly: with a byte source and six outcomes, four outcomes get one extra chance each out of 256. Every value in the uneven tail is discarded here and redrawn. The source here is 32 bits rather than 8, so the discarded slice is a billionth of it and the loop essentially never runs twice — the cost of exact uniformity is a comparison.

Questions people actually ask

What is modulo bias, and does it matter?
Take a random byte — 0 to 255 — and reduce it modulo 6 to pick a die face. 256 does not divide by 6: it is 42 sixes with 4 left over, so four of the faces get 43 chances out of 256 and two get 42. That is a 2.4% edge on the low faces, invisible in any amount of casual rolling and decisive in a lottery, a shuffle or a security token. This page throws away the leftover tail and draws again, which is called rejection sampling and makes every outcome exactly equally likely.
Is Math.random good enough?
For a raffle at a party, yes. For anything an adversary would care about, no: it is a pseudo-random sequence — V8 uses xorshift128+ — and given enough consecutive outputs its internal state can be recovered and every future value predicted. It is also not seeded from a source you control. The numbers here come from crypto.getRandomValues instead, which draws from the operating system’s entropy pool.
What does "all different" actually do?
It draws without replacement, the way numbered balls come out of a machine: once 17 has been drawn it cannot come again. With the box unticked each number is drawn independently, so repeats happen — and they happen far more often than people expect. Draw six numbers from 1 to 100 with replacement and there is about a 14% chance two of them match.
Are these numbers sent anywhere?
No. The draw happens in your browser, on your machine, and no request is made when you press Generate. There is nothing on our side to log, which is worth knowing if you are drawing something you would rather not have sitting in a server access log.
Can I use this to pick lottery numbers?
You can, and it will do exactly what it says: draw distinct numbers uniformly from your range. It will not improve your odds, because nothing can. What it does do is avoid the two ways a careless generator would quietly skew your picks toward the low end of the range.

Sources

Related