0
Q:

js random

//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
Math.floor((Math.random() * 100) + 1);
//Generate random numbers between 1 and 100
//Math.random generates [0,1)
10
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
var RandomNumber = Math.ceil(Math.random() * 100);              
// Returns a random number between 1 and 100
5
Math.floor(Math.random() * 10);
11
function random(min, max) {
  return ~~(Math.random() * (max - min + 1) + min);
}
random(1, 5);
3
var randomNo = Math.floor(Math.rand() * 10000000001);
console.log(randomNo);

//This will generate random number
0

New to Communities?

Join the community