数学对象:math
Math对象的属性:
-
math.PI=== π === 180°
math.PI/180*60 -
Math.LN2 2的自然对数。
Math对象的方法:
-
求最大值和最小值:不需要处理数据类型,会自动转换。
Max():
Min(): -
向上向下取整
-
floor():向下取整
66.9999999
alert(Math.floor(66.9999999))
2.ceil():向上取整
66.0000001
alert(Math.ceil(66.0000001))
-
math.random():生成随机数0-1之间的随机数。生成数包含0但是不包括1.
-
想要获取0-x之间的随机整数
var test=Math.floor(Math.random()*X); -
想要100-200之间的随机数。
Math.random*(最大值-最小值)+最小值。
Var test=Math.floor(Math.random()*(200-100)+100);
- 生成随机颜色:
R(0-255) G(0-255) B(0-255)
- 随机验证码:
- function suiji(){
var flag=“asdfghjklpoiuytrewqzxcvbnmwsxrfvyhnikmujtgb”
var newsuzu=new Array();
for(var i=0;i<4;i++){
var suzu=flag.split("");
var a=Math.floor(Math.random()*flag.length);
var sub= suzu[a];
newsuzu[newsuzu.length]=sub;
flag=flag.replace(sub,"");
}
document.write("
for(var i=0;i<newsuzu.length;i++){
var r=Math.floor(Math.random()*256);
var g=Math.floor(Math.random()*256);
var b=Math.floor(Math.random()*256);
var q=newsuzu[i]
document.write("<span style='color: rgb("+r+","+g+","+b+")'>"+q+"</span>")
}
document.write("</div>")
}