How this is calculated
Values come from the Web Crypto API's getRandomValues, which fills an array with cryptographically strong random bytes supplied by the operating system's entropy pool. Those bytes are then mapped onto your chosen range.
The mapping avoids modulo bias. Naively taking a random byte modulo the range size makes the lower values slightly more likely whenever the range does not divide evenly into 256 — a small distortion, but a real one, and it is exactly the kind of flaw that matters in a draw. Rejection sampling discards values that would fall in the biased region and draws again.
Uniqueness, when requested, is enforced by tracking values already drawn and redrawing on collision. The range must be at least as large as the count, or the request is impossible to satisfy.
Cryptographic randomness versus Math.random
Math.random in a browser is fast and statistically reasonable for simulations and animations, but it is not unpredictable. Implementations use algorithms such as xorshift128+, whose internal state can be recovered from a modest number of observed outputs — after which every subsequent value is known.
For a dice roll in a game that does not matter. For a prize draw, a random sample that must withstand scrutiny, or anything where a participant benefits from predicting the outcome, it does. This generator uses the crypto source in all cases, because the performance difference is irrelevant at these volumes and the security difference is not.
What randomness does not guarantee
A genuinely random sequence will contain runs, repeats and clusters that look non-random to human intuition. Six consecutive numbers in a lottery draw are exactly as likely as any other specific combination, and a truly random ten-number draw quite often contains a repeat.
The opposite is also true: sequences that have been adjusted to look random — evenly spread, no repeats, no runs — are not random. If you need to avoid repeats for practical reasons, use the unique option rather than regenerating until the output looks right, which introduces exactly the bias you were trying to avoid.
How to use the random number generator
- Set the minimum and maximum. Both ends are inclusive. Any integer range works, including negative values.
- Choose how many numbers you need. For a unique draw, the range must contain at least as many values as you are requesting.
- Decide whether values must be unique. Unique for draws and sampling without replacement. Leave it off for independent rolls, where repeats are expected and correct.