The formula for random numbers Math.random() * (max - min) + min
is the correct one to get a uniformly distributed number between min
and max
.
max - min
will give you the range in which you want to generate the random numbers. So in this case 1000 - 100
results in a range of 900
.
Multiplying by Math.random()
will give you a random number in the range. So, with a Math.random()
producing 0.5
after multiplying you get 450
.
Finally, adding min
back to the random pick ensures the number you get is within bounds of min
and max
.
For example Math.random()
produces 0.01
if we substitute in the formula we get 0.01 * (1000 - 100) = 9
which is below min
. Conversely, if Math.random()
produces 1
then 1 * (1000 - 100) = 900
which is the highest random number possible to get from the range and yet it's still below max
. In both cases adding min
to the result ensures the random number you get is within max
and min