How to Generate (Truly) Random Numbers
Math.random() vs crypto.getRandomValues(), and two measured pitfalls: byte % 6 makes four of six outcomes 2.4% more likely (10 million draws), and drawing 10 numbers from 1-100 repeats one 37% of the time.
Pseudo-Random vs Cryptographic
Math.random() is a fast pseudo-random generator: V8 (Chrome, Edge, Node) and Firefox both use xorshift128+, whose next outputs can be computed from a handful of observed ones. That is fine for shuffling cards in a game or jittering an animation, and wrong for anything someone could gain from predicting: passwords, tokens, giveaway draws. crypto.getRandomValues() draws from the operating system's cryptographically secure generator, which is designed so that past outputs do not reveal future ones.
The Bug That Survives Good Randomness: Modulo Bias
Secure random bytes can still give unfair results if they are mapped to a range carelessly. A byte has 256 values; 256 is not divisible by 6, so byte % 6 makes 0-3 slightly more likely than 4-5. We drew 10 million bytes with crypto.getRandomValues() in Node 22 (October 14, 2026; script in our test folder):
| Method | Share of each face 0-5 |
|---|---|
byte % 6 | 16.78, 16.81, 16.80, 16.78, 16.42, 16.41% (theory: 16.80% four times, 16.41% twice) |
| Rejection sampling (discard bytes ≥ 252, then % 6) | 16.65, 16.67, 16.68, 16.65, 16.68, 16.67% |
A 2.4% relative edge for four of six outcomes is small per draw but real in a draw with stakes. Rejection sampling removes it at the cost of discarding 1.6% of bytes. Our Random Number Generator uses 53-bit draws from crypto.getRandomValues() with rejection sampling for integers, so every value in the range is equally likely; we checked its distribution with a chi-square test when we rebuilt it.
Repeats Are More Likely Than You Think
With duplicates allowed, each draw is independent, so repeats are common long before the range runs out. The probability of at least one repeat when drawing k numbers from 1 to n:
| Draw | Chance of a repeat |
|---|---|
| 23 from 365 (the birthday problem) | 50.7% |
| 10 from 100 | 37.2% |
| 5 from 50 | 18.6% |
| 50 from 5,000 | 21.8% |
So for raffles, samples or anything where each number stands for a different person, switch on unique numbers: the tool then draws without replacement (Floyd's algorithm), like drawing cards from a deck.
Running a Fair Giveaway
- Number the entries 1 to N in a list you publish or keep, before the draw.
- Draw with a cryptographic generator, unique numbers if there are several winners.
- Record the draw (a screenshot or screen recording with the time) and publish the method; for public draws, a verifiable source such as a published beacon value can be used as the seed.
Two people using a cryptographic generator can still get the same numbers by chance; what they cannot do is predict or reproduce each other's draws. For lists of names rather than numbers, the Sort Lines tool can shuffle a list.
Try the tool
Cryptographically secure random numbers with custom range and options.
Frequently Asked Questions
Is Math.random() truly random?
What's the difference between pseudo-random and cryptographic random?
Can two people generate the same random numbers?
How many unique numbers can I generate from a range?
Is shuffle mode different from generate mode?
Published Apr 15, 2026 · Updated Oct 13, 2026 by Derek Giordano
Browse all utility tools →