1// Between any two numbers
2Math.floor(Math.random() * (max - min + 1)) + min;
3
4// Between 0 and max
5Math.floor(Math.random() * (max + 1));
6
7// Between 1 and max
8Math.floor(Math.random() * max) + 1;
1function getRandomInt(min, max) {
2 return Math.floor(Math.random() * (max - min)) + min;
3}
1function getRandomNumberBetween(min,max){
2 return Math.floor(Math.random()*(max-min+1)+min);
3}
4
5//usage example: getRandomNumberBetween(20,400);
6
1/**
2* Gets random int
3* @param min
4* @param max
5* @returns random int - min & max inclusive
6*/
7getRandomInt(min, max) : number{
8 min = Math.ceil(min);
9 max = Math.floor(max);
10 return Math.floor(Math.random() * (max - min + 1)) + min;
11}
1function getRandomIntInclusive(min, max) {
2 min = Math.ceil(min);
3 max = Math.floor(max);
4 return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive
5}
1function GetRandom(max){
2 return Math.floor(Math.random() * Math.floor(max))
3}
4
5GetRandom(3); //returnval 0, 1, 2