Skip to content
thecalculatorstudio

Random Number Generator: Fair Random Numbers in Any Range

Set a minimum and maximum, how many numbers you want and whether repeats are allowed, then press Generate. The numbers are drawn in your browser, with no bias towards any value.

Last reviewed

Press Generate to draw your numbers. Each press draws a fresh set.

What this generator does

Choose a minimum, a maximum and how many numbers you want, press Generate, and you get that many random numbers in the range, ends included. You can allow repeats or ask for all-different numbers, sort the results, ask for decimals, and copy the list.

The numbers are made in your browser, when you press the button. They are not made when the page loads, so a link you share carries your settings and not a set of numbers.

How fair it is

Fairness has two parts: where the randomness comes from, and how it is turned into a number in your range.

The source. The generator uses your browser’s cryptographic random-number source, the same one used for security tasks. It is designed to be unpredictable, which is more than an everyday “random” function promises.

The conversion. This is where many generators go wrong. A random byte is a number from 0 to 255. To get a number from 0 to 9 the easy way is to divide by 10 and keep the remainder, which is called modulo. It is not fair:

  • 256 = 25 × 10 + 6, so the remainders 0, 1, 2, 3, 4 and 5 can each come from 26 different bytes.
  • The remainders 6, 7, 8 and 9 can only come from 25.
  • So 0 to 5 are 26 ÷ 25 = 1.04 times as likely as 6 to 9, 4% more.

This generator removes the bias by rejection. Bytes from 250 to 255 are thrown away and redrawn, which leaves exactly 250 usable bytes, 25 for each of the ten values. Rejection happens 6 times in 256, about 2.3% of the time, and every value in your range is then exactly equally likely. The same idea works for any range, using more bytes when the range is large.

Ranges as wide as ±1,000,000,000,000,000 are handled exactly.

No repeats

Choose “No repeats” and every number in the set is different. The generator uses an efficient method, Floyd’s algorithm, that picks different values without listing the whole range, and then shuffles the result so the order is random as well.

You cannot ask for more different values than the range holds. Six different numbers from 1 to 5 is impossible, and the generator says so.

With repeats, how likely is a repeat? Draw 5 numbers from 1 to 10 with repeats allowed. The chance that all five are different is (10 × 9 × 8 × 7 × 6) ÷ 10⁵ = 30,240 ÷ 100,000 = 0.3024. So the chance that at least one number comes up twice is 1 − 0.3024 = 69.76%. Repeats are the usual result, not a sign that something is wrong. It is the same effect as the birthday problem: among 23 people there is a better than even chance, about 50.7%, that two share a birthday.

Decimals

Choose from 1 to 6 decimal places for decimals. The generator draws a whole number from 0 up to (maximum − minimum) × 10 to the power of the places, and scales it. Each value on that grid is equally likely and the numbers are exact, with no stray digits.

From 0 to 1 with 2 places there are 101 possible values, 0.00 to 1.00. The minimum and maximum may use no more decimal places than you ask for. 0.55 to 2 with one place is refused, because 0.55 is not on a grid of tenths.

Some uses

  • Picking a winner from a numbered list. Number the entries, draw one number in that range, and you have the winner.
  • Sampling. Draw several different numbers to choose which rows of a spreadsheet to check.
  • Games. For a die, use 1 to 6. For a coin, use 1 to 2.
  • Choosing a team order. Draw all the numbers from 1 to the number of players with no repeats, and that is the order.
  • Test data. Use many numbers, sorted or not, to fill a table.

Common mistakes about randomness

  • The gambler’s fallacy. A number that has not appeared for a while is not “due”. Each draw is independent, so the 21st draw is as likely to be any value as the first. Three sixes in a row on a die has a probability of (1/6)³ = 0.46%, but the next roll is still one in six.
  • Reading patterns into runs. Real random data contains streaks and repeats. Data that never repeats would be the suspicious kind.
  • Using it where it is not enough. For a lottery, a casino or a cryptographic key, use a method that is audited and certified for the purpose.

For a list of numbers in a pattern, use the number sequence calculator. To find the average and spread of the numbers you have drawn, paste them into the average calculator or the statistics calculator. For very large numbers, the big number calculator is there.

How it is calculated

A random integer below n is found by drawing just enough random bytes to cover n, reading them as one number, and redrawing if it falls at or above the largest multiple of n that fits, then taking the remainder. Decimals scale an integer on a grid of 10 to the power of the places. Different values are chosen by Floyd’s algorithm and shuffled with the Fisher–Yates method using the same unbiased draw. Counts may be from 1 to 1,000, and the sum is worked in exact decimals.

Frequently asked questions

Are the numbers truly random?

They come from your browser's cryptographic random source, which is designed to be unpredictable, and are turned into numbers with no bias. That makes them suitable for picking a winner or a sample. For anything where fairness must be audited, such as a lottery, use a certified method.

What is modulo bias, and does this have it?

It is the slight favouring of small numbers you get from taking a random value modulo the range size. A byte is 0 to 255; split into a range of 10 it gives the values 0 to 5 a chance of 26 in 256 and 6 to 9 only 25, which is 4% less. This generator throws away the uneven top slice, so there is none.

How do I get numbers with no repeats?

Choose "No repeats". The numbers are then all different, and the generator tells you if you ask for more than the range holds: you cannot draw 6 different numbers from 1 to 5.

Can I generate decimals?

Yes. Choose from 1 to 6 decimal places. Each value on that grid is equally likely, so from 0 to 1 with 2 places there are 101 possible values, 0.00 to 1.00. The minimum and maximum may have no more decimal places than you choose.

Does a number that has not come up become more likely?

No. Each draw is independent. A number that has not appeared in 20 draws is exactly as likely as any other on the 21st. Believing otherwise is the gambler's fallacy.

Are my numbers stored or sent anywhere?

No. They are made in your browser when you press Generate and are not sent to a server. A shared link carries your settings, not your numbers.

Related calculators

Sources