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 randomInRange(min, max)
2{
3 return Math.floor(Math.random() * (max - min + 1) + min);
4}
1Math.floor(Math.random() * (max - min + 1)) + min;
2
3// Between 0 and max
4Math.floor(Math.random() * (max + 1));
5
6// Between 1 and max
7Math.floor(Math.random() * max) + 1;
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