Home / Blog / Math & Scientific

Math & Scientific

Choose a Random Integer from an Inclusive Range

Set inclusive random-number bounds, count the possible values, and distinguish repeated sampling from unique selection and reproducible test data.

Choose a Random Integer from an Inclusive Range

Write the interval before generating a value

A request for a random number from a range needs two decisions: whether the values must be integers and whether the endpoints count. This guide uses whole numbers and includes both endpoints. For a range from 12 through 18, the possible results are 12, 13, 14, 15, 16, 17 and 18. That is seven choices, not six. The distance between the endpoints is six, but counting the integers includes the starting value as well. Write “12 through 18 inclusive” when handing the task to someone else.

Enter minimum, maximum and count separately

In the linked Random Number Generator, enter 12 as Min, 18 as Max and 5 as Count to request five selections from that interval. The count specifies how many output values you want; it does not change the range or divide it into five sections. A valid sample could be 12, 18, 14, 14, 16. The repeated 14 is allowed because each selection uses the full interval again. Treat this as an illustrative possible sample, not a promised output: a new run normally produces a different sequence.

Check equal and negative bounds

An interval can contain negative values. From −3 through 3 there are seven possible integers, including zero. From −8 through −4 there are five, even though both endpoints are negative. If minimum and maximum are both 7, the only possible answer is 7, so any requested count consists entirely of sevens. A reversed interval such as minimum 9 and maximum 2 is an input error. Swap the bounds deliberately if that was your intention; do not rely on an application to silently reinterpret them.

Distinguish an arbitrary list from a continuous interval

Suppose the allowed choices are 10, 20 and 50. Entering minimum 10 and maximum 50 would also permit 11, 12 and many other unwanted values. Instead, assign indices 1, 2 and 3 to your three choices, generate one index, and look up the associated value. This gives each list entry one place in the selection process. If a value appears twice in the list, those two entries give it extra weight. Decide whether you are sampling distinct labels or separate entries before constructing the mapping.

Understand replacement and the chance of a repeat

Two independent selections from 1 through 10 have 100 ordered outcomes under the ideal uniform model. Ten outcomes are doubles: 1 then 1, 2 then 2, and so on. The chance of a match is therefore 10/100, or 10%. Requesting ten values does not guarantee that every possible value appears once. If you need an ordering of ten different items, use a shuffle or a without-replacement method. Removing duplicates afterward can shorten the result and changes the procedure you originally described.

Keep numerical limits and text formatting separate

The tool accepts safe-integer endpoints between −9007199254740991 and 9007199254740991, and the inclusive span must also be a safe integer. It accepts a count from 1 through 10000. These limits avoid pretending that rounded JavaScript numbers remain exact integers. A numerical result does not preserve leading zeros: 42 and 000042 are the same number. If you need a fixed-width label, define a formatting step separately. Do not present a padded label as a guaranteed unique identifier just because it looks structured.

Use randomness appropriate to the job

The local generator uses Math.random, which MDN documents as noncryptographic. It is suitable for ordinary examples and informal sampling, with no exposed seed or replay control. For a repeatable test, record a fixture sequence or use a deliberately seeded generator in your own test environment. Keep the minimum, maximum and count alongside saved values so that the original selection rule remains understandable. For secret tokens, use a security design built around appropriate cryptographic randomness instead. A few successful runs verify the chosen bounds and output shape; they do not prove perfect statistical uniformity, independence across every browser, or suitability for an audited prize draw.

Use the Random Number Generator

Open Random Number Generator

Source and scope

MDN: Math.random and inclusive integer ranges