Math.random()
// will return a number between 0 and 1, you can then time it up to get larger numbers.
//When using bigger numbers remember to use Math.floor if you want it to be a integer
Math.floor(Math.random() * 10) // Will return a integer between 0 and 9
Math.floor(Math.random() * 11) // Will return a integer between 0 and 10
// You can make functions aswell
function randomNum(min, max) {
return Math.floor(Math.random() * (max - min)) + min; // You can remove the Math.floor if you don't want it to be an integer
}
Math.random() // Returns number from 0 to 1.
Math.floor(Math.random() * max) // Return number from 0 to max minus 1
// Example
Math.floor(Math.random() * 10) // Return number from 0 to 9
// Add a +1 to return a number from 0 to max
Math.floor(Math.random() * 10) + 1 // Returns Number from 1 to 10
// To have a min and max
Math.floor(Math.random() * (max - min)) + min
// Example
Math.floor(Math.random() * (20 - 10)) + 10
/*
`Math.random` returns a pseudo-random number between 0 and 1.
a pseudo-random number is generated by an algorithm, it is not
technically actually random, but for all intents and purposes
it is random enough that no human should be able to find a
pattern
*/
Math.random(); // -> Decimal number between 0 and 1
Math.round(Math.random()); // -> 0 or 1
Math.random() * max; // -> Decimal number between 0 and max
Math.floor(Math.random() * max); // -> Whole number between 0 and max - 1
Math.round(Math.random() * max); // -> Whole number between 0 and max
Math.ceil(Math.random() * max); // -> Whole number between 1 and max
(Math.random() * (max - min)) + min; // Decimal number between min and max
Math.floor((Math.random() * (max - min)) + min); // Whole number between min and max - 1
Math.round((Math.random() * (max - min)) + min); // Whole number between min and max
Math.ceil((Math.random() * (max - min)) + min); // Whole number between min + 1 and max
Math.random() * Math.random(); // Decimal number between 0 and 1 with a tendency to be smaller
1 - Math.random() * Math.random(); // Decimal number between 0 and 1 with a tendency to be larger
/* If 1 argument is given, minimum will be set to 0 and maximum to this argument
* If 2 arguments were given, the fist would be the minimum and the second the maximum
* The function will return an integer in [min, max[
*/
const Math.randint = function (min,max) {
[min,max] = (max===undefined)?[0,min]:(min>max)[max,min]:[min,max];
return Math.floor(Math.random*(max-min)+min);
}
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min; // max & min both included
}
/* If 1 argument is given, minimum will be set to 0 and maximum to this argument
* If 2 arguments were given, the fist would be the minimum and the second the maximum
* The function will return an integer in [min, max[
*/
const Math.randint => (min,max) {
[min,max] = (max===undefined)?[0,min]:(min>max)[max,min]:[min,max];
return Math.floor(Math.random*(max-min)+min);
}
Math.floor(Math.random() * 10) + 1 // Random number Between 1 and 10
// First Math.random give us a random number between 0 and 0,99999
// The we multiply it by 10
// And we round dow with Math.floor
// We add 1 so the result will never be 0
// Another Example:
h.floor(Math.random() * 20) + 10 // Random number Between 10 and 20
// Retorna un número aleatorio entre min (incluido) y max (excluido)
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
// using array and random example
function getTheBill() {
let names = ["bob", "mike", "matt"];
let randomFriend = Math.floor(Math.random() * names.length);
let randomFriendBuyng = names[randomFriend];
return randomFriendBuyng + " is buying us lunch today!";
}