xxxxxxxxxx
// Generate a random number between 1 and 10
const randomNumber = Math.floor(Math.random() * 10) + 1;
console.log(randomNumber);
xxxxxxxxxx
function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
xxxxxxxxxx
Math.floor((Math.random() * 100) + 1);
//Generate random numbers between 1 and 100
//Math.random generates [0,1)
xxxxxxxxxx
var random;
var max = 8
function findRandom() {
random = Math.floor(Math.random() * max) //Finds number between 0 - max
console.log(random)
}
findRandom()
xxxxxxxxxx
Math.floor(Math.random() * (max - min + 1)) + min;
//max is the highest number you want it to get
//min is the lowest number you want it to get
xxxxxxxxxx
// 0 -> 10
Math.floor(Math.random() * 11);
// 1 -> 10
Math.floor(Math.random() * 10) + 1;
// 5 -> 20
Math.floor(Math.random() * 16) + 5;
// -10 -> (-2)
Math.floor(Math.random() * 9) - 10;
xxxxxxxxxx
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
xxxxxxxxxx
Math.random()
// Or something between 0 and 9:
Math.floor(Math.random() * 10)
// You can even make functions:
function random(min, max){return Math.floor(Math.random() * (max - min)) + min}
xxxxxxxxxx
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
xxxxxxxxxx
// Generate a random number between min (inclusive) and max (exclusive)
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
// Generate a random number between 1 and 10
const randomNumber = getRandomNumber(1, 11);
console.log(randomNumber);