1.获取随机数Math.random()
[m,n]
Math.floor(Math.random() * (n-m +1)+m)
1. Math.random() 获取0到1之间的随机数[0,1)
var aa=Math.random();
console.log(aa);

2. 获取[0,n]之间的随机整数
Math.floor()对小数取整
Math.floor(Math.random()*(n+1)
- 例如获取
0到6之间的随机整数
console.log(Math.floor(Math.random()*(6+1)));

3. 获取[m,n]之间的随机整数
var num = Math.floor(Math.random() * (n-m +1)+m)
2.Math.max()、Math.min()
二者用于确定一组数值中的最小值和最大值,这两个方法都接收任意多个参数,避免使用额外的循环和
if语句来确定一组数值的最大最小值。
let max=Math.max(3,54,32,16)
let min=Math.min(3,54,32,16)
console.log(max,min);//54 3
- 数组中的最大值使用扩展运算符
...
let values=[1,22,44,66,88]
console.log(Math.max(...values));//88 使用扩展运算符遍历数组
3.舍入方法
Math.ceil()方法始终向上舍入为最接近的整数Math.floor()方法始终向下舍入为最接近的整数Math.round()方法执行四舍五入Math.fround()方法返回数值最接近的单精度32位浮点值表示
console.log(Math.ceil(22.9));//23
console.log(Math.ceil(22.2));//23
console.log(Math.floor(33.1));//33
console.log(Math.floor(33.9));//33
console.log(Math.round(44.1));//44
console.log(Math.round(44.9));//45
console.log(Math.fround(0.1));//0.10000000149011612
版权声明:本文为weixin_45803990原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。