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.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 random(min, max) {
2 return ~~(Math.random() * (max - min + 1) + min);
3}
4random(1, 5);
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
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 = function (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}
1var randomNo = Math.floor(Math.rand() * 10000000001);
2console.log(randomNo);
3
4//This will generate random number