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.
1Math.floor((Math.random() * 100) + 1);
2//Generate random numbers between 1 and 100
3//Math.random generates [0,1)
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}
1function random(min, max) {
2 return ~~(Math.random() * (max - min + 1) + min);
3}
4random(1, 5);
1/**
2 * Returns a random number between min (inclusive) and max (exclusive)
3 */
4function getRandomArbitrary(min, max) {
5 return Math.random() * (max - min) + min;
6}
7
8/**
9 * Returns a random integer between min (inclusive) and max (inclusive).
10 * The value is no lower than min (or the next integer greater than min
11 * if min isn't an integer) and no greater than max (or the next integer
12 * lower than max if max isn't an integer).
13 * Using Math.round() will give you a non-uniform distribution!
14 */
15function getRandomInt(min, max) {
16 min = Math.ceil(min);
17 max = Math.floor(max);
18 return Math.floor(Math.random() * (max - min + 1)) + min;
19}
20