0
Q:

random js

//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.
25
function randomNumber(min, max) {
  return Math.floor(Math.random() * (max - min)) + min + 1;
}
5
Math.floor(Math.random() * 6) + 1  
14
var random;
var max = 8
function findRandom() {
  random = Math.floor(Math.random() * max) //Finds number between 0 - max
  console.log(random)
}
findRandom()
7
function getRandomNumberBetween(min,max){
    return Math.floor(Math.random()*(max-min+1)+min);
}

//usage example: getRandomNumberBetween(20,400); 
25
function randomNumber(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}
11
//Returns a number between 1 and 0
  console.log(Math.random());
  
//if you want a random number between two particular numbers, 
//you can use this function
  function getRandomBetween(min, max) {
    return Math.random() * (max - min) + min;
  }
//Returns a random number between 20 and 170
  console.log(getRandomBetween(20,170));
  
//if you want a random integer number from one number to another 
//(including the min and the max numbers), you can use this function
  function getRandomIntBetween(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }
  
//Returns a random integer number from 0 to 25
  console.log(getRandomIntInclusive(0,25));
6
// Genereates a number between 0 to 1;
Math.random();

// to gerate a randome rounded number between 1 to 10;
var theRandomNumber = Math.floor(Math.random() * 10) + 1;
1
function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive 
}
4
Math.floor(Math.random() * 10);
11
function random(min, max) {
  return ~~(Math.random() * (max - min + 1) + min);
}
random(1, 5);
3

New to Communities?

Join the community