Utility · Apr 15, 2026 · Updated Oct 13, 2026 · 4 min read

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):

MethodShare of each face 0-5
byte % 616.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:

DrawChance of a repeat
23 from 365 (the birthday problem)50.7%
10 from 10037.2%
5 from 5018.6%
50 from 5,00021.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

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.

Open Random Number Generator →

Frequently Asked Questions

Is Math.random() truly random?
No — it's pseudo-random, generated from a deterministic algorithm. It looks random enough for casual uses (games, shuffling) but is predictable enough to be unsafe for security, cryptography, or financial stakes.
What's the difference between pseudo-random and cryptographic random?
Pseudo-random (Math.random) uses fast algorithms but is predictable if you observe enough outputs. Cryptographic random (crypto.getRandomValues) uses OS entropy sources that make prediction computationally infeasible.
Can two people generate the same random numbers?
Yes, by chance, especially from small ranges: two people each picking a number from 1-10 match 10% of the time. With a seeded pseudo-random generator and the same seed they will match every time. A cryptographic generator cannot be seeded or predicted, so draws cannot be reproduced on purpose.
How many unique numbers can I generate from a range?
As many as the range holds: you cannot draw 11 unique integers from 1-10, and the tool will tell you. Unique draws use Floyd's algorithm, so a few numbers from a huge range (say 10 from 20 million) are fast and use little memory.
Is shuffle mode different from generate mode?
This tool generates numbers from a range. If you need to shuffle existing items (like a list of names), use our Sort Lines tool with the shuffle option — it preserves your items while randomizing order.

Published Apr 15, 2026 · Updated Oct 13, 2026 by Derek Giordano

📚 References & Further Reading
Browse all utility tools →
⚡Random Number Generator→⚡Random Design Generator | Ultimate Design Tools→
UtilityComplete Guide to Barcode Types & When to Use Each→UtilityHow to Design Professional Business Cards for Free→GuideHow Fancy Text Works (And When Not to Use It)→DeveloperHow to Convert Between Binary, Octal, Decimal, and Hex→DeveloperHow to Create a Markdown Table (Syntax & Generator)→ColorHow to Generate a Color Palette from Scratch→ColorHow to Generate Shades and Tints of Any Color→ImageHow to Generate a Favicon for Your Website→