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.
| Result of byte mod 6 | Bytes that produce it | Chance |
|---|---|---|
| 0, 1, 2, 3 | 43 each | 43 in 256 |
| 4, 5 | 42 each | 42 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
- How RNGs WorkWhere computers get randomness, the difference between hardware and algorithmic generators, how browsers expose them, and which suits which job.
- Random vs. PseudorandomWhat makes a number sequence pseudorandom, how seeds and periods work, a tiny generator you can follow by hand, and when predictability matters.
Open the tool
Jump into Random Number Generator when you are ready to process your files.
