获取指定区间范围随机数,包括lowerValue和upperValue
function randomFrom(lowerValue,upperValue)
{
return Math.floor(Math.random() * (upperValue - lowerValue + 1) + lowerValue);
}
//如获取2-6之间的随机数
alert(randomFrom(2,6));
//如获取1-10之间的随机数
alert(randomFrom(1,10));
//如获取1-100之间的随机数
alert(randomFrom(1,100));
Math.floor() 返回小于或等于一个给定数字的最大整数。
Math.floor() === 向下取整
例子
Math.floor( 45.95);
// 45
Math.floor( 45.05);
// 45
Math.floor( 4 );
// 4
Math.floor(-45.05);
// -46
Math.floor(-45.95);
// -46