Math.round(Math.random()*(y-x))+x
Math.random()产生0到1之间的任意小数
Math.round() 四舍五入
Math.ceil() 向上取整
Math.floor() 向下取整
本文介绍了如何使用JavaScript生成指定范围内的随机整数。通过Math.random()生成0到1之间的随机小数,结合Math.round()实现四舍五入,最终达到在指定区间内获取随机数的目的。
Math.round(Math.random()*(y-x))+x
Math.random()产生0到1之间的任意小数
Math.round() 四舍五入
Math.ceil() 向上取整
Math.floor() 向下取整

被折叠的 条评论
为什么被折叠?