Mash.random() 随机数
定义
随机获取范围内的一个数 (精确到小数点后14位)
生成指定范围内的随机数
- 生成 小于
x的随机数(包含小数)
Math.random() * x
js
Math.random() * 60 // 生成 60 以内的随机数- 生成 小于
x的随机整数
parseInt(Math.random() * x)
js
parseInt(Math.random() * 60) // 生成 60 以内的随机整数- 生成
向下取整的随机整数
使用 Math方法下的 floor属性 进行舍弃小数 向下取整.
向上取整 : ceil属性
Math.floor(Math.random() * x)
js
Math.floor(Math.random() * 60) // 57生成两个数之间的随机数
- 生成
m ~ n之间的随机整数 (包括n与m)
Math.floor(Math.random() * (m - n)) + n
js
Math.floor(Math.random() * (8 - 100)) + 100 // 8 ~ 100