I tried this function to find a random int number between two bonds, I know the method of Math.floor () and Math.random()
how works, but I could not understand the logic with this function (max-min + 1)
and the addition of min
. why do we need to use (max-min+1)
then the addition ofmin
?
function random(min, max) {
const num = Math.floor(Math.random() * (max - min + 1)) + min;
return num;
}
console.log(random(5, 10));