Math.random()
是令零碎随机选取大于等于0.0
且小于1.0
的伪随机double
值Math.random()*99
是生成一个大于等于0
且小于99
的随机数Math.floor(x)
返回小于参数x
的最大整数,即对浮点数向下取整,因而能够用Math.floor(Math.random()*99)
取得随机整数
想要生成大于等于5且小于12的随机数,应用5+Math.random()*(12-5);
封装成js办法如下
function getRandomValue(minValue, maxValue) { return Math.floor(Math.random() * (maxValue - minValue) + minValue);}