random int between two numbers javascript
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(getRandomInt(5, 10));
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(getRandomInt(5, 10));
This code defines a function called getRandomInt that takes two parameters, min and max.
Within the function, the min value is increased by 1 using Math.ceil method and the max value is decreased by 1 using Math.floor method.
The function then returns a random integer within the range specified by the min and max values.
Finally, the console.log statement calls the function and outputs the random integer between 5 and 10 to the console.