javascript get random number
snippet in javascript

how to generate a random number in javascript

user1502

//To genereate a number between 0-1
Math.random();
//To generate a number that is a whole number rounded down
Math.floor(Math.random())
/*To generate a number that is a whole number rounded down between
1 and 10 */
Math.floor(Math.random() * 10) + 1 //the + 1 makes it so its not 0.

js random number

user3152

var random;
var max = 8
function findRandom() {
  random = Math.floor(Math.random() * max) //Finds number between 0 - max
  console.log(random)
}
findRandom()

javascript random number

user4613

var RandomNumber = Math.ceil(Math.random() * 100);              
// Returns a random number between 1 and 100

javascript get random number

user4297

// Returns a number between min and max
function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}

random js

user5762

function randomNumber(min, max) {
  return Math.floor(Math.random() * (max - min)) + min + 1;
}

javascript get random number

user5624

// 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); 
}