1//To genereate a number between 0-1
2Math.random();
3//To generate a number that is a whole number rounded down
4Math.floor(Math.random())
5/*To generate a number that is a whole number rounded down between
61 and 10 */
7Math.floor(Math.random() * 10) + 1 //the + 1 makes it so its not 0.
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// Returns an integer between min and max (the maximum is exclusive and the minimum is inclusive)
2function getRandomInt(min, max) {
3 min = Math.ceil(min);
4 max = Math.floor(max);
5 return Math.floor(Math.random() * (max - min) + min);
6}
7
1let object = random(0, 50);
2// making "object" a random number between 0 and 50.
3
4console.log(object);
5// printing object in the console