How do you generate a random integer in a range?
Pick a minimum and a maximum, and every whole number from one to the other, inclusive, should be equally likely. This tool does that with up to 100 numbers per click. It draws 32-bit values from your browser's cryptographically secure random source (two of them combined into a 53-bit value when the range holds more than about 4.3 billion numbers) and throws away any value that would make some results more common than others.
Settings and output
| Setting | Default | Notes |
|---|---|---|
| Minimum | 1 | Negative numbers work; decimals are rounded down |
| Maximum | 100 | Must be greater than or equal to the minimum |
| Numbers | 1 | Slider from 1 to 100 |
Results appear one per line, which pastes cleanly into a spreadsheet column or a text file. A blank Minimum or Maximum uses the default, while a typed 0 is used as 0. If the minimum is larger than the maximum you get the message "Min must be less than or equal to max." and no output.
Example: rolling dice
Set Minimum to 1, Maximum to 6 and Numbers to 10, then click Generate. You get ten lines, each a single digit from 1 to 6, for example:
3
3
4
4
3
5
1
5
3
3
That run came straight from the tool, and yes, it rolled five 3s. Real randomness is lumpier than people expect. With only six faces and ten rolls, repeats are guaranteed, and streaks like this are common. To simulate 2d6, generate two numbers per roll and add them; don't set the range to 2 to 12, because that makes 2 as likely as 7.
Modulo bias, and why this tool avoids it
The quick way to turn a random 32-bit value into a die roll is value % 6 + 1. The catch is that 2³² (4,294,967,296) isn't a multiple of 6. It leaves a remainder of 4, so faces 1 to 4 each get one more possible input than faces 5 and 6. With 32-bit inputs the skew is tiny, but with a single random byte (256 values) it's easy to see: faces 1 to 4 would come up 43 times in 256 and faces 5 and 6 only 42.
The fix is rejection sampling:
- Work out the largest block of 32-bit values (53-bit for very wide ranges) that divides evenly by the range size.
- Draw a value from
crypto.getRandomValues. - If it falls in the leftover tail above that block, discard it and draw again.
- Otherwise take the remainder and add the minimum.
Because the kept block is an exact multiple of the range size, every result has exactly the same number of raw values behind it, so the output is exactly uniform, not just close. Discards are rare for normal ranges, and even in the worst case at least half of all draws are kept, so a click never takes more than a moment.
The random source itself is the browser's CSPRNG. MDN describes it as a pseudo-random generator seeded with enough entropy to be suitable for cryptographic purposes, the same family of design that NIST SP 800-90A standardises as deterministic random bit generators.
Edge cases
| What you enter | What happens |
|---|---|
Minimum or maximum 0 |
Used as 0 |
| Blank minimum or maximum | Falls back to 1 or 100 |
2.9 to 5.9 |
Rounded down to 2 to 5 |
Text such as abc |
Error: "Min and max must be numbers." |
| Range wider than 2^53 values | Error: "The range is too wide..." |
| Minimum greater than maximum | Error: "Min must be less than or equal to max." |
Good uses
- Sampling records for QA. Generate 20 row IDs between 1 and your table's highest ID and spot-check those rows.
- Splitting test users. Assign each tester a number from 1 to 2 (or 1 to 4) for A/B or multi-variant groups.
- Picking a page, question or seat. Anything numbered works, from audit pages to exam questions.
- Dice and board games when the physical dice have gone missing.
When you need numbers without repeats, paste a numbered list into the random name picker, or put the list in random order with the text lines shuffler. For values with decimal places, such as prices or measurements, the random decimal generator works on the same principle. If you specifically need a prime, the random prime generator picks one uniformly from every prime in a range.
Last updated