Why most "random" number code is subtly biased
The obvious way to generate a random number in a range is
min + Math.random() * (max - min), or for integers,
value % range. Both have real problems. Math.random() is
not cryptographically secure and, in older engines, has measurably uneven bit
distribution. The modulo approach has a subtler flaw: unless the range divides the
generator's output space evenly, some numbers land more often than others —
an effect called modulo bias.
This generator avoids both problems. It draws from
crypto.getRandomValues(), the same cryptographically secure source
browsers use for encryption keys, and uses rejection sampling to
eliminate modulo bias entirely: any draw that would fall in the uneven leftover
region is discarded and redrawn. Every number in your range ends up with exactly
equal probability.
How rejection sampling works
range = max − min + 1 limit = 232 − (232 mod range) draw a random 32-bit value; if it's ≥ limit, discard and draw again result = min + (accepted value mod range) limit is always an exact multiple of range, so once a
draw is below it, every possible remainder from 0 to range−1 has occurred the
same number of times. Rejected draws happen rarely — for small ranges,
well under 1% of draws — and cost nothing but a fast, invisible retry.
Decimals use the same algorithm, scaled
Rather than a separate method for non-integer results, decimal mode rounds your bounds to the chosen number of decimal places, multiplies by a power of ten to turn the range into whole numbers, runs the identical unbiased draw, and divides back down. A request for "0.4 to 85.8, 1 decimal place" becomes "4 to 858" internally — so decimal results inherit the same bias-free guarantee as whole numbers.
A note on very large ranges
JavaScript's Number type cannot represent every integer beyond
±9,007,199,254,740,991 (253−1). Beyond that point, some
values become indistinguishable from their neighbors, which would silently make
"random" results inaccurate. This calculator checks not just that your minimum and
maximum are individually valid, but that the span between them also fits
safely — a huge symmetric range like −9 quadrillion to +9 quadrillion
can have two perfectly valid endpoints while the distance between them is still too
large to generate accurately. If that happens, you'll see a clear message asking
you to narrow the range.
Common uses
| Use case | How to set it up |
|---|---|
| Picking a raffle or giveaway winner | Whole numbers, min 1 to your number of entries |
| Random sample for a survey or experiment | Whole numbers with an exclude list for anyone already sampled |
| Simulating a die or spinner | Whole numbers, e.g. 1–6 or 1–20 |
| Random price, weight, or measurement for testing | Decimals, with the precision matching your real data |
| Team or task assignment | Whole numbers, min 1 to the number of people or tasks |
Frequently asked questions
Is this generator truly random?
It uses your browser's cryptographically secure random number generator (the Web Crypto API), the same source used for encryption. Combined with rejection sampling to remove modulo bias, every number in your chosen range has exactly equal probability of being selected.
What is modulo bias?
It's a subtle skew that appears when a random number generator's output range doesn't divide evenly into the range you want. Numbers near the low end of your range end up very slightly more likely than numbers near the high end. It's small enough to be invisible in casual use, but it's real — this calculator avoids it entirely with rejection sampling instead of a plain modulo operation.
Can I generate a random decimal number?
Yes — switch to the Decimals tab, set your minimum, maximum, and how many decimal places you want. A range like 0.4 to 85.8 with 1 decimal place will return results like 47.3, evenly spread across that grid of possible values.
Can I generate a random number from a negative range?
Yes, both whole numbers and decimals support negative minimums and maximums — for example −50 to 50, or −10.5 to 10.5. If you accidentally enter the minimum larger than the maximum, they're swapped automatically.
How does excluding numbers work?
List the numbers to avoid, separated by commas. The generator draws as normal and simply discards and redraws any result that matches your excluded list, so the remaining numbers stay equally likely relative to each other.
Need a two-sided random outcome instead? Try the Coin Flipper or the Dice Roller.