How to Generate Random Numbers Without Bias
Learn to generate random numbers fairly: why Math.random falls short, what modulo bias is and how rejection sampling fixes it. Tested on 70 million numbers.
By TheTextKit TeamPublished

A table of three results for the random number generator. 73,860,000 numbers were drawn and tested. 14 of 14 ranges were proven exactly fair by feeding in every possible byte string. The chi-square p-values ran from 0.08 to 0.96.
To generate random numbers fairly, take random bytes from a secure source, turn them into a whole number, and throw away any value that would make some results more likely than others. Then add your minimum. The shortcut of taking a random number and keeping the remainder after dividing by the range looks harmless and is not: it favours the low values. This guide explains why, how the fix works, how to pick numbers with no repeats, and how we tested our random number generator on more than 70 million numbers.
How to generate random numbers
Open the Random Number Generator and follow these steps to generate random numbers.
- Enter the minimum and the maximum. Both are included, so 1 and 6 can give 1, 2, 3, 4, 5 or 6.
- Choose how many numbers you need, up to 10,000.
- Choose whole numbers or a number of decimal places.
- Tick No repeats if the same number must not come up twice, as in a draw.
- Press Generate. Choose Draw again for a fresh set, and copy the result with a comma, a space or a new line between the numbers.
The presets cover the usual cases: a die, a d20, 1 to 10, six numbers from 49 without repeats, and a decimal between 0 and 1.
Why Math.random is not enough
Most code starts with Math.random(). According to MDN, it returns a pseudo-random number from 0 up to but not including 1, with approximately uniform distribution. MDN also says it does not provide cryptographically secure random numbers, and that you should not use it for anything related to security. The seed is chosen by the implementation, and you cannot choose or reset it.
For a fair draw or anything that matters, the better source is the Web Crypto API. MDN says crypto.getRandomValues gives cryptographically strong random values. It has one limit that matters when you ask for a lot of numbers. It throws an error if the array is larger than 65,536 bytes, so large requests have to be filled in pieces. Our generator does that, and it can return 10,000 numbers at once.
The modulo bias trap
Here is the classic mistake. You have one random byte, which is a whole number from 0 to 255, and you want a die roll from 1 to 6. So you write byte % 6 + 1. But 256 does not divide by 6. It is 42 groups of 6 with 4 left over, so the results 1 to 4 each occur 43 times in 256 and the results 5 and 6 occur only 42 times.
| Die face | Times it occurs in 256 bytes |
|---|---|
| 1 | 43 |
| 2 | 43 |
| 3 | 43 |
| 4 | 43 |
| 5 | 42 |
| 6 | 42 |

A table of how often each die face occurs when one random byte is taken modulo 6. Faces 1, 2, 3 and 4 occur 43 times in 256. Faces 5 and 6 occur 42 times.
The gap is small here, about 2.4%, but a test sees it. On 6 million rolls, a fair die gives each face about a million. The biased one would give each of the first four faces about 1,007,800 and each of the last two about 984,400. A chi-square test scores that at around 732, against a fair-die limit near 11. With bigger ranges the damage grows. For a range of 200, one byte gives the first 56 values two chances each and the other 144 values one chance, so the low values are twice as likely.
The Wikipedia article on the Fisher-Yates shuffle describes the same problem. When you apply the modulo operator to a random number to fit a range, the bias is systematically in favour of small remainders. The suggested fix is to discard out-of-range values and retry.
Rejection sampling: the fix
The fix is to use only as many random values as divide evenly into the range, and draw again for the rest.
- Work out how many random bytes cover the range. One byte holds 256 values, two bytes hold 65,536, and so on.
- Find the largest multiple of the range that fits in that space. For a range of 6 and one byte, that is 252.
- Draw the bytes as a number. If it is 252 or more, throw it away and draw again.
- Otherwise, take the remainder after dividing by the range.
Now every result has exactly 42 ways to occur, because 252 is 42 times 6. The cost is an occasional redraw.
| Range | Bytes drawn | Share redrawn |
|---|---|---|
| 6 | 1 | 1.6% |
| 100 | 1 | 21.9% |
| 1,000 | 2 | 0.8% |
| 6,000 | 2 | 8.4% |

A table of how often a random draw is thrown away to keep every result equally likely. A range of 6 redraws 1.6%. A range of 100 redraws 21.9%. A range of 1,000 redraws 0.8%. A range of 6,000 redraws 8.4%.
Picking numbers with no repeats
A draw without repeats has two good methods. If you want many numbers from a small range, shuffle the whole range and take the first few. The Fisher-Yates shuffle does this, and the same Wikipedia article says it produces an unbiased permutation, where every permutation is equally likely, as long as the random numbers it uses are unbiased. That is why each swap in our tool uses the rejection method above.
If you want a few numbers from a huge range, such as 2,000 from 1 to a billion, shuffling a billion values would be silly. Instead you draw a number, skip it if you have seen it, and repeat. Repeats are rare when the range is large, so this is fast. The tool picks the method for you. If you ask for more numbers than the range holds, say 11 different numbers from 1 to 10, it explains why that is impossible.
Decimals and very large ranges
To generate random numbers with decimals, the tool draws from an exact grid. A range from 0 to 1 with two decimals has 101 possible values, from 0.00 to 1.00, and each is equally likely. The tool does not draw a float and round it, which would make the two ends only half as likely as the others. Whole numbers are handled exactly up to 40 digits, using BigInt arithmetic, so a range from 1 to 10 to the power of 30 works as well as 1 to 6.
How we tested the generator
We tested fairness in three ways.
First, an exact proof for small ranges. We replaced the random source with a machine that hands out every possible byte string once, in order. If the method is fair, every result must come out exactly the same number of times. For 14 ranges, including 3, 6, 7, 100, 255, 1,000, 65,536 and 65,537, which needed all 16,777,216 three byte strings, each result appeared exactly the same number of times.
Second, a statistical test on the real source. We drew millions of numbers and ran a chi-square test. A fair generator gives p-values spread between 0 and 1, and a biased one gives a tiny p-value.
| Range | Numbers drawn | p-value |
|---|---|---|
| 1 to 6 | 6,000,000 | 0.17 |
| 1 to 100 | 10,000,000 | 0.82 |
| 1 to 1,000 | 10,000,000 | 0.54 |
| 1 to 6,000 | 12,000,000 | 0.18 |

A table of the chi-square p-values for four ranges of random numbers. 6,000,000 numbers from 1 to 6 gave 0.17. 10,000,000 from 1 to 100 gave 0.82. 10,000,000 from 1 to 1,000 gave 0.54. 12,000,000 from 1 to 6,000 gave 0.18.
We ran the same test on 1 to 2, 1 to 10, 0 to 255 and 0 to 256, which sit on the edges of one byte, and every p-value was between 0.28 and 0.96. We also checked that consecutive rolls are independent, by counting the 36 possible pairs of die faces over 1.5 million pairs. The p-value was 0.76.
Third, the no repeats method. For picks such as 2 numbers from 3 and 5 from 5, we counted every possible ordered result over 600,000 to 1.2 million draws, 6, 24 and 120 possible results. All appeared, and the p-values were 0.75, 0.08, 0.89 and 0.39.
| Test | Count | Result |
|---|---|---|
| Ranges proven exactly fair | 14 | All equal |
| Chi-square tests on one number at a time | 9 | p from 0.17 to 0.96 |
| Chi-square tests on unique draws | 4 | p from 0.08 to 0.89 |

A table of three kinds of tests of the random number generator. 14 ranges were proven exactly fair. 9 chi-square tests on single numbers gave p-values from 0.17 to 0.96. 4 chi-square tests on draws without repeats gave p-values from 0.08 to 0.89.
In total about 73.9 million numbers were drawn in the statistical tests. A passing test does not prove randomness, only that these checks found no bias.
Limits
The numbers come from the browser's secure random source. MDN notes that the Web Cryptography specification mandates no minimum degree of entropy, and that browsers are urged to provide the best entropy they can, so the quality depends on your browser and system. It is right for games, draws, sampling and tests. It is not a licensed lottery or gambling system, and for passwords use a Password Generator, which is built for that job.
Frequently asked questions
How do I generate a random number between two values?
Draw random bytes from a secure source, turn them into a whole number, and reject any value that would favour some results, then add the minimum. With the tool, type the minimum and maximum and press Generate. Every value in the range is equally likely.
What is modulo bias?
It is the unevenness you get when you take a random number and keep the remainder after dividing by the size of the range. Unless the source range divides evenly, some results come up more often. One random byte taken modulo 6 gives the values 1 to 4 a count of 43 in 256 and the values 5 and 6 a count of 42.
Is Math.random good enough for a random number generator?
For games and demos it is fine, but MDN says it does not provide cryptographically secure random numbers and should not be used for anything related to security. For fairness and security, use crypto.getRandomValues, which is meant for strong random values.
How do I pick random numbers with no repeats?
Either shuffle the whole range and take the first few, which is the Fisher-Yates method, or draw numbers and skip any you have already picked. The first is better when you want more than half of a small range, and the second when you want a few from a huge range.
Can I use an online random number generator for passwords?
Use a tool made for passwords instead. A password needs enough length and a mix of characters, and the result should never be reused. A number generator gives you digits in a range, which is a smaller set of choices than a password generator offers.
Generate your random numbers now
Set your range in the Random Number Generator to generate random numbers, with or without repeats, and copy the result. For averages and spread of a list of numbers, use the Mean, Median and Mode Calculator. Everything runs in your browser, so nothing you generate is uploaded.


