How to pick a random integer in a range
To pick a random integer in a range, enter the smallest acceptable value as Min and the largest as Max. Both ends are included, and every integer between them has the same chance. This page shares its engine with the Random Number Generator, which starts at 1 to 100 with repeats allowed and is the quicker option for everyday picks. Come here when the range itself is the question: which ends count, how many values it holds and how to write the same logic in code. For signed test data and duplicate behaviour, see the Random Integer Generator.
Inclusive range semantics
The number of possible results is always Max − Min + 1. The "+ 1" is where most mistakes start, so it helps to check a few cases:
- 1 to 10 contains ten values; 0 to 10 contains eleven.
- -5 to 5 contains eleven values, because zero is one of them.
- 5 to 5 is a valid range of one value; asking for three numbers without Unique values returns
5 5 5. - Decimal bounds are rounded inward: Min rounds up and Max rounds down, so 0.5 to 3.5 means 1 to 3.
- Min larger than Max is rejected with an error rather than swapped silently.
Dice-style and lottery-style ranges
The Unique values setting decides which kind of draw you get. Unticked, every pick is independent and repeats can occur, like rolling a die again and again. Ticked, a value that has already come up is discarded and drawn again, like pulling numbered balls out of a drum.
| Scenario | Min | Max | How many | Unique values |
|---|---|---|---|---|
| One six-sided die | 1 | 6 | 1 | Off |
| Twenty-sided die, five rolls | 1 | 20 | 5 | Off |
| Coin toss as 0 or 1 | 0 | 1 | 1 | Off |
| Six numbers from 59 | 1 | 59 | 6 | On |
| Random order of 1 to 10 | 1 | 10 | 10 | On |
Real output for six unique numbers from 1 to 59, sorted ascending with a comma separator: 7, 21, 24, 25, 36, 43. With the page defaults (1 to 10, ten numbers, unique) you get every value once in random order, for example 6 7 3 8 10 5 4 9 1 2.
One common trap: picking from 2 to 12 is not the same as rolling two dice. A range makes all eleven totals equally likely, while real dice produce 7 far more often than 2 or 12. To model the sum, generate two rolls from 1 to 6 and add them.
Off-by-one pitfalls when you code a random range
Programming languages do not agree on whether the upper bound is included, so the same range needs different arguments:
// JavaScript: both ends included
Math.floor(Math.random() * (max - min + 1)) + min
// Node.js: crypto.randomInt excludes the second argument
crypto.randomInt(1, 7) // 1 to 6
# Python
random.randint(1, 6) # 1 to 6, stop included
random.randrange(1, 7) # 1 to 6, stop excluded
secrets.randbelow(6) + 1 # 1 to 6, secure
// PHP: both ends included
random_int(1, 6);Two classic bugs in hand-written JavaScript:
- Dropping the + 1:
Math.floor(Math.random() * (max - min)) + minnever returnsmax. - Using Math.round:
Math.round(Math.random() * (max - min)) + minincludes both ends but gives each of them half the chance of the values in between.
Note that Math.random() is fine for games and simulations but is not designed for security. This page reads from crypto.getRandomValues and uses rejection sampling, so each value in the range is exactly equally likely.
Checking your own range code
A quick test catches most range bugs: draw a few thousand values from a small range and look at the extremes. Generate 5,000 values from 1 to 6 here, sort them ascending and confirm that the list starts at 1 and ends at 6. Do the same with your implementation. If the maximum never appears, the + 1 is missing; if the first and last values turn up only about half as often as the others, something is rounding instead of flooring.
Limits of the range
Each run returns 1 to 10,000 integers, and Max − Min may not exceed 9,007,199,254,740,991. With Unique values on, the count cannot be larger than the range. To draw names instead of numbers, the Random Name Picker or Shuffle Lines work on a list directly. All draws happen in your browser.