Math是一个对象,不像Date 要new Date()
属性
Math.PI
方法
ceil(x) | 对数进行上舍入。 |
floor(x) | 对 x 进行下舍入。 |
pow(x,y) | 返回 x 的 y 次幂。 |
console.log(Math.pow(2,4)) // 16
round(x) | 四舍五入。 |
random() | 返回 0 ~ 1 之间的随机数。(0,1) |
/*
* Math.random()
* - 可以用来生成一个0-1之间的随机数
* - 生成一个0-10的随机数
* - 生成一个0-x之间的随机数
* Math.round(Math.random()*x)
*
* - 生成一个1-10
* - 生成一个x-y之间的随机数
* Math.round(Math.random()*(y-x)+x)
*/
Math.round(Math.random()*10);//[0,10]
var b = Math.round(Math.random()*6)+2;//[2,8]