1function getRandomNumberBetween(min,max){
2 return Math.floor(Math.random()*(max-min+1)+min);
3}
4
5//usage example: getRandomNumberBetween(20,400);
6
1const rnd = (min,max) => { return Math.floor(Math.random() * (max - min + 1) + min) };
1const min = 1;
2const max = 4;
3const intNumber = Math.floor(Math.random() * (max - min)) + min;
4console.log(intNumber); //> 1, 2, 3
1function getRandomInt(min, max) {
2 min = Math.ceil(min);
3 max = Math.floor(max);
4 return Math.floor(Math.random() * (max - min + 1)) + min;
5}
1Math.floor(Math.random() * 10) + 1 // Random number Between 1 and 10
2// First Math.random give us a random number between 0 and 0,99999
3// The we multiply it by 10
4// And we round dow with Math.floor
5// We add 1 so the result will never be 0
6
7// Another Example:
8h.floor(Math.random() * 20) + 10 // Random number Between 10 and 20
1function getRandomIntInclusive(min, max) {
2 min = Math.ceil(min);
3 max = Math.floor(max);
4 return Math.floor(Math.random() * (max - min + 1)) + min; // max & min both included
5}