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);
}

corn6
4 声望0 粉丝

寻找自我Corn6