CCalclabhub

Work

Integer draw and range width

Adding one to the width before truncating makes both endpoints reachable, so an integer draw from 1 to 100 can return either 1 or 100.

Last updated

The Formula

integer value = floor(min + u x (max - min + 1))

Adding one to the width before truncating makes both endpoints reachable, so an integer draw from 1 to 100 can return either 1 or 100.

SymbolMeaning
minMinimum
maxMaximum
vDrawn integer

What Each Symbol Means

minvalue

Minimum

The smallest integer the draw may return.

maxvalue

Maximum

The largest integer the draw may return.

vvalue

Drawn integer

The truncated result.

See It Worked With Real Numbers

A formula on its own does not tell you which number goes where. The how to generate random numbers page applies this equation to a concrete case, shows every intermediate step, and names the mistakes that produce a wrong answer even when the algebra is right.

Where This Formula Appears

This equation is the working core of How To Generate Random Numbers, filed under Everyday Life. Every number the guide produces comes from substituting into this one line.

Common Questions

What is the formula for Integer draw and range width?

The formula for Integer draw and range width is integer value = floor(min + u x (max - min + 1)). Adding one to the width before truncating makes both endpoints reachable, so an integer draw from 1 to 100 can return either 1 or 100.

What units should I use with Integer draw and range width?

Substitute min in value, max in value, v in value. Convert every input to those units before you start, because the equation only returns the right answer when the units on both sides agree.

What is a common mistake when using Integer draw and range width?

Forbidding repeats on a narrow range. Filtering out repeats needs the range to be wider than the count. Asking for fifty unique values from 1 to 20 cannot be satisfied, and a tight case makes the rejection loop very slow before it gives up.