How to Generate Random Numbers in a Range Without Repeats
Sampling without replacement removes each selected integer from the eligible range before the next integer is selected.
A worked example with numbers
A teacher needs five distinct question numbers from 1 through 20. Generate a random order of the 20 integers and take its first five values, for example 17, 4, 20, 9, 12. Every listed value is in the range and appears once. A named value such as 17 has 5/20 = 25% chance of appearing somewhere among the five positions and 1/20 = 5% chance of being first.
How to set the rule before the result
Use the unique random numbers tool for a no-repeat result, and use the integer range generator when repeats are acceptable. State whether endpoints are included: 1–20 has 20 integers, while a half-open programming interval [1,20) has 19 integers.
Common mistakes that change the odds or the process
Do not request 21 unique integers from 1 through 20; the request is impossible. Do not generate one independent integer five times and delete repeats after viewing them unless that rejection procedure is part of the implementation. Do not mistake 0–19 for 1–20: both contain 20 values but represent different labels.
Where this method stops being appropriate
Unique sampling is suitable for assigning distinct seats, question numbers, or informal draw positions. It is not a substitute for eligibility checks, an official raffle process, or a rule that needs weights, exclusions, or meaningful ranking.
How the random source fits into the rule
MDN’s Math.random reference documents that JavaScript random floats begin at 0 inclusive and end below 1; integer-range code must explicitly map that half-open source to its announced endpoints. The crucial no-repeat fact is removal from the eligible set, not a visually different series of numbers.
Check the actual population and denominator
A teacher needs five distinct question numbers from 1 through 20. Generate a random order of the 20 integers and take its first five values, for example 17, 4, 20, 9, 12. Every listed value is in the range and appears once. A named value such as 17 has 5/20 = 25% chance of appearing somewhere among the five positions and 1/20 = 5% chance of being first.
Set the rule before an output exists
Use the unique random numbers tool for a no-repeat result, and use the integer range generator when repeats are acceptable. State whether endpoints are included: 1–20 has 20 integers, while a half-open programming interval [1,20) has 19 integers.
Keep a different process from slipping in
Do not request 21 unique integers from 1 through 20; the request is impossible. Do not generate one independent integer five times and delete repeats after viewing them unless that rejection procedure is part of the implementation. Do not mistake 0–19 for 1–20: both contain 20 values but represent different labels.
Limit the conclusion to this stated case
Unique sampling is suitable for assigning distinct seats, question numbers, or informal draw positions. It is not a substitute for eligibility checks, an official raffle process, or a rule that needs weights, exclusions, or meaningful ranking.
Name the source behind the numerical claim
MDN’s Math.random reference documents that JavaScript random floats begin at 0 inclusive and end below 1; integer-range code must explicitly map that half-open source to its announced endpoints. The crucial no-repeat fact is removal from the eligible set, not a visually different series of numbers.
Check capacity and endpoints first
| Requested interval | Eligible integers | Maximum unique output count |
|---|---|---|
| 1–20 inclusive | 20 | 20 |
| 0–19 inclusive | 20 | 20 |
| 1–19 inclusive | 19 | 19 |
Equal capacity does not mean equal labels. Write the inclusive endpoints next to the output so a person can check both membership and uniqueness.
Audit the finished series
For five requested values, count five outputs, verify every value lies between 1 and 20, then compare each pair for a duplicate. Those three checks prove the stated no-repeat output contract. They do not prove that the values suit a real-world allocation with hidden constraints.
Choose an order when positions have meaning
Five unique values can be an unordered set or five ranked positions. If the first drawn number wins a different prize from the fifth, retain generation order. If the values only identify five questions, sorting 4, 9, 12, 17, 20 afterward is harmless but must not be mistaken for the draw order. The selection rule is uniform over ordered samples; presentation order is a separate decision.
Work through the last available value
When a request takes 20 unique values from 1 through 20, the final value is no longer random after the first 19 values are known: it is the one remaining label. That is expected, not a defect. The complete ordered output is still a uniformly selected permutation of all 20 values. Conversely, asking for only 19 values leaves one unselected value, and each original number has 1/20 chance of being that omitted value. These complementary checks are useful for classroom exercises because they expose the removal rule without relying on a black-box animation.
For a printed worksheet, write the original interval and requested count at the top of the page. A later reader can then distinguish a valid five-number no-repeat selection from five independent values that merely happen not to collide. Include the draw date and whether display order has any prize meaning.