Illustration of the range 1 to 6 with the formula maximum minus minimum plus one equals six possible numbers

Calculators & everyday math

How to Pick a Random Integer Between Two Numbers

Picking a random whole number between two numbers sounds simple, but two details trip people up: counting the range correctly and mapping random values onto it evenly. This guide covers both, with negative ranges and spreadsheet functions along the way.

Ready to try the tool this guide describes?

Count the range correctly

When both ends are included, a range holds max − min + 1 numbers. 1 to 6 holds 6 numbers, 0 to 99 holds 100, and −10 to 10 holds 21. Forgetting the +1 is the classic off-by-one error that makes the maximum impossible to draw.

The usual formula

With a random decimal u from 0 up to (but not including) 1, such as Math.random() gives: result = min + floor(u × (max − min + 1)). For 1 to 6, u = 0.5 gives 1 + floor(3) = 4.

Modulo bias

Another common method takes a random integer and uses the remainder after dividing by the range size. That is only fair when the size divides evenly into the number of possible random values. A random byte has 256 values, and 256 = 42 × 6 + 4, so byte mod 6 gives 0, 1, 2, and 3 forty-three times each but 4 and 5 only forty-two times.

Modulo bias comparison
Result of byte mod 6Bytes that produce itChance
0, 1, 2, 343 each43 in 256
4, 542 each42 in 256

The fix: rejection sampling

Throw away any random value that falls in the last, incomplete block and draw again. For bytes and a range of 6, reject 252 through 255; the remaining 252 values split into exactly 42 of each result. The Random Number Generator does the same with 53-bit values, where a redraw is almost never needed.

In a spreadsheet

=RANDBETWEEN(1, 100) returns a whole number from 1 to 100, both included. It recalculates whenever the sheet changes, so copy and paste the value if you need to keep it.

Random Integer in a Range FAQ

How many numbers are between 1 and 100, inclusive?
100, because 100 − 1 + 1 = 100.
Can the range include negative numbers?
Yes. −10 to 10 has 21 numbers, including 0, and the same formula works.
What is modulo bias?
The small unfairness when a range size doesn’t divide evenly into the number of possible random values, so a remainder favors the lower results.
Is RANDBETWEEN inclusive?
Yes. Both the bottom and top values can be returned.
Why did I get the same number twice in a row?
Each draw is independent, so repeats happen — in a range of 10, about 1 draw in 10 repeats the one before.

Related guides

Open the tool

Jump into Random Number Generator when you are ready to process your files.

← Back to all guides

More from Looty

Explore Looty’s Ecosystem

Discover more ways Looty can help you learn, organize, create, and make an impact.