I've been using this code to generate a random number with js:
var max = 10;
Math.floor( Math.random() * ( max + 1 ) );
From what I understand that will generate a number from 0 to 10, but what if I want to generate a random number from 1 to 10? or from 5 to 10?
try this:
function getRandomInt(min, max){
return Math.floor(Math.random() * (max - min + 1)) + min;
}
You do from 0 to 9, then you add one to the result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With