JaveScript基础 Math 方法

1、.random() 随机数[ 0 , 1 )的随机16位小数

例:

alert( Math.random() ) ==>0.254148875646....

如果要随机得到: 0~10之间;
alert ( Math.floor( Math.random() *10)) ==>0到10

如果要随机得到:10~20
alert ( Math.random() *10 + 10 ) ==>12.25454148875646....

如果要取10~100的数
console.log( Math.random()*(100 - 10) +10 );
如果要取50~500的数
console.log( Math.random()*(500 - 50 ) + 50 );

封装通用式
function random( x , y ){
  console.log( Math.random()*( y - x ) + x );
}

取整

.round() 四舍五入
.ceil () 向上取整 (小数部分不是0 就是加1) ceil天花板的意思
.floor() 向下取整 (小娄部分全部舍弃)

几个几何相关的

.sin() 正弦
.cos() 余弦
.tan() 正切
.PI ==> π 3.1415925.... 如果要得到1/2π Math.PI*0.5
.abs 绝对值 :就是得到正数 2 ==> 2       -3 ==> 3
.max( a , b , c , d , f ) 所有参数里面取最大值
.mix( a , b , c , d , f ) 所有参数里面取最小值

你可能感兴趣的:(JaveScript基础 Math 方法)