503 B
503 B
We need to "map" all values from the interval 0..1 into values from min
to max
.
That can be done in two stages:
- If we multiply a random number from 0..1 by
max-min
, then it the interval of possible values increases0..1
to0..max-min
. - Now if we add
min
, the possible interval becomes frommin
tomax
.
The function:
function random(min, max) {
return min + Math.random() * (max - min);
}
alert( random(1, 5) );
alert( random(1, 5) );
alert( random(1, 5) );