To get a random number is JS you almost always rely on the function Math.random
. The only issue is that it returns
a number between 0 and 1.
You can generate a number in a specific range by writing a straightforward function.
function getRandomNumberInRange(min, max) {
return Math.random() * (max - min) + min;
}
The function getRandomNumberInRange
returns a random number between min
(inclusive) and max
(exclusive).