xxxxxxxxxx
//Write the following code to get a random number between 0 and n
Math.floor(Math.random() * n);
xxxxxxxxxx
function getRandomNumberBetween(min,max){
return Math.floor(Math.random()*(max-min+1)+min);
}
//usage example: getRandomNumberBetween(20,400);
xxxxxxxxxx
const random_number = Math.floor(Math.random() * 10) + 1; //Bettween 1 and 10
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
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
// Returns an integer between min and max (the maximum is exclusive and the minimum is inclusive)
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min);
}
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
let randomNum = Math.floor(Math.random() * 5)
return( 0 or 1 or 2 or 3 or 4)
let randomNum = Math.floor(Math.random() * 5) + 1
return( 1 or 2 or 3 or 4)
// * 5 in this code meaning a number between 0 and 4
xxxxxxxxxx
// Returns a number between min and max
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}