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 getRandomNumberBetween(min,max){
2 return Math.floor(Math.random()*(max-min+1)+min);
3}
4
5//usage example: getRandomNumberBetween(20,400);
6
1function randomNumber(min, max) {
2 return Math.floor(Math.random() * (max - min)) + min;
3}
1Math.floor(Math.random() * 10) + 1 // Random number Between 1 and 10
2// First Math.random give us a random number between 0 and 0,99999
3// The we multiply it by 10
4// And we round dow with Math.floor
5// We add 1 so the result will never be 0
6
7// Another Example:
8h.floor(Math.random() * 20) + 10 // Random number Between 10 and 20
1// using array and random example
2function getTheBill() {
3 let names = ["bob", "mike", "matt"];
4
5 let randomFriend = Math.floor(Math.random() * names.length);
6
7 let randomFriendBuyng = names[randomFriend];
8
9 return randomFriendBuyng + " is buying us lunch today!";
10}
11