var myRand = Math.random(); // Get a random number from 0 to 1
var zeroToSix = myRand * 6; // Get a random number from 0 to 6, not including 6
var zeroToFive = Math.floor(zeroToSix); //Floor it, maiking it 0 to 5
var oneToSix = zeroToFive + 1; // Guarantees that it will be 1 to 6
console.log(oneToSix); //Result

//or, quite simply:

var result = Math.floor(Math.random() * 6) + 1;
console.log(result);

Explanation: Math.random() has a chance to return 0. If we use Math.ceil(0) we may still get 0, which will result in the range of 0 to 6, not 1 to 6, and may mess up our program. So the proper way is Math.floor() + 1 Credit: David McFarland