Joe
0
Q:

how to generate a random number between 1 and 6 in javascript

//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
Math.floor(Math.random() * 6) + 1  
14
function randomNumber(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}
11
Math.floor(Math.random() * (max - min + 1)) + min;

// Between 0 and max
Math.floor(Math.random() * (max + 1));

// Between 1 and max
Math.floor(Math.random() * max) + 1;
0
Math.floor(Math.random() * 10) + 1
3
Math.floor(Math.random() * 3);
5
var randomNum ;
randomNum = Math.ceil(Math.random() * 100);
document.write("Random number between 1 and 100: " + randomNum)
-2

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

New to Communities?

Join the community