Q:

get random numbers javascript

Math.floor((Math.random() * 100) + 1);
//Generate random numbers between 1 and 100
//Math.random generates [0,1)
10
// Returns a number between min and max
function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}
1
Math.floor(Math.random() * 10);
11
Math.floor(Math.random() * 10) + 1
3
//Write the following code to get a random number between 0 and n
Math.floor(Math.random() * n);
2
// 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); 
}
0

Math.random()
1

New to Communities?

Join the community