1Math.random()
2// will return a number between 0 and 1, you can then time it up to get larger numbers.
3//When using bigger numbers remember to use Math.floor if you want it to be a integer
4Math.floor(Math.random() * 10) // Will return a integer between 0 and 9
5Math.floor(Math.random() * 11) // Will return a integer between 0 and 10
6
7// You can make functions aswell
8function randomNum(min, max) {
9 return Math.floor(Math.random() * (max - min)) + min; // You can remove the Math.floor if you don't want it to be an integer
10}
1function getRandomNumberBetween(min,max){
2 return Math.floor(Math.random()*(max-min+1)+min);
3}
4
5//usage example: getRandomNumberBetween(20,400);
6
1function getRandomNumberBetween(min,max){
2 return Math.floor(Math.random()*(max-min+1)+min);
3}
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 getRandomInt(min, max) {
2 min = Math.ceil(min);
3 max = Math.floor(max);
4 return Math.floor(Math.random() * (max - min + 1)) + min;
5}
1/* If 1 argument is given, minimum will be set to 0 and maximum to this argument
2 * If 2 arguments were given, the fist would be the minimum and the second the maximum
3 * The function will return an integer in [min, max[
4 */
5const Math.randint => (min,max) {
6 [min,max] = (max===undefined)?[0,min]:(min>max)[max,min]:[min,max];
7 return Math.floor(Math.random*(max-min)+min);
8}