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 The Math.random() function returns a floating-point, pseudo-random
3 number in the range 0 to less than 1 (inclusive of 0, but not 1)
4 with approximately uniform distribution over that range — which you
5 can then scale to your desired range. The implementation selects the
6 initial seed to the random number generation algorithm; it cannot
7 be chosen or reset by the user.
8*/
9function getRandomInt(max) {
10 return Math.floor(Math.random() * Math.floor(max));
11}
12
13console.log(getRandomInt(3));
14// expected output: 0, 1 or 2
15
16console.log(getRandomInt(1));
17// expected output: 0
18
19console.log(Math.random());
20// expected output: a number from 0 to <1
1function getRandomNumberBetween(min,max){
2 return Math.floor(Math.random()*(max-min+1)+min);
3}
4getRandomNumberBetween(50,80);
5
6
1
2Math.floor(Math.random() * 11); // returns a random integer from 0 to 10
3Math.floor(Math.random() * 10) + 1; // returns a random integer from 1 to 10
1function uniqueNumber(count) {
2 let defaultNumber = 4413277523420
3 let convertToArray = defaultNumber.toString().split("")
4 let sliceNumber = convertToArray.slice(0, count)
5 let randomNumber = Math.floor((Math.random() * +sliceNumber.join("")));
6
7 if (randomNumber.toString().split("").length < count) {
8 randomNumber = Math.abs(randomNumber - +sliceNumber.join(""))
9 }
10
11 return randomNumber
12}
1// The below code executes 0-100, I have used es6 arrow function concept
2setInterval(() => console.log(Math.floor(Math.random()*101)),1000);
3