js获取范围内随机整数的实现

**

js获取范围内随机整数的实现

**
示例: Math.floor(Math.random()*(10-5) + 5)

说明:Math.floor(参数a)
Math.floor(参数a)是获取参数a的整数部分,即取整数。

说明:Math.random()
Math.random()是取0~1之间的随机数。
Math.random()*10是取0~10之间的随机数。

*搭配使用:Math.floor(Math.random()10) => 取0~10之间的随机整数

*稍微复杂点:Math.floor(Math.random()10 + 1 =》取1~11之间的随机整数

要取n~m范围之间的随机整数:Math.floor(Math.random()*(m-n) + n)

end

如果对你有帮助,记得点个赞噢(~~)

猜你喜欢

转载自blog.csdn.net/start_sea/article/details/128256866