、、、、、、、、、、、仅对以前所学做复习记录使用、、、、、、、、、
Math是一个对象,不是一个构造函数,所以不能new Math
一、返回0到1之间的随机数值,但不包含1,包含零。顾头不顾尾。一般零出现的概率极小,可以忽略不计。
<div class="bg" @click="cssChange"> </div>
cssChange(){
console.log(Math.random());
}
Math.random()*10的意思:因为random的取值范围是[0,1),所以0x10=0,1x10=10;取的是0-10之间的数字。
二、Math.floor()是向下取整数(向越小的值方向取值),即将小数点后面的数字全部省去;如果是个整数就取它本身的值:
// 向越小的值方向取值
Math.floor(1.222) // 1
Math.floor(1.9999999) // 1
Math.floor(1) // 1
Math.floor(-1) // -1
Math.floor(-1.222) // -2
三、获取x到y的随机整数,包含xy,用Math.round(),公式:
Math.round(Math.random()*(y-x)+x);
例如获取0-10
<div class="bg" @click="cssChange"> </div>
cssChange(){
console.log(Math.round(Math.random()*(10-0)+0));
}
四、用Math.floor()取x到y的值,包含xy,公式:
Math.floor(y+1-Math.random()*(y+1-x))
例如:取5-7
methods:{
cssChange(){
console.log(Math.floor(8-Math.random()*(8-5)));
}
}