第08章 数学对象
8.1 数学对象简介
Math.属性
Math.方法
8.2Math对象的属性
PI
LN2
LN10
LOG2E
LOG10E
SORT2
SORT1_2
120*Math.PI/180 //120度,牢记
8.3Math对象的方法
max(a,b,...n)
min(a,b,...n)
random()
8.4最大值与最小值
Math.max(a,b,...n)
Math.min(a,b,...n)
8.5取整运算
向下取整:floor()
向上取整:ceil()
8.6三角函数
sin(x)
cos(x)
tan(x)
...
8.7生成随机数
Math.random()
8.8生成随机验证码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>生成4位随机验证码</title>
</head>
<body>
<script>
var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
var arr = str.split("");
var result = "";
for (var i=0; i<4; i++)
{
var n = Math.floor(Math.random() * arr.length);
result += arr[n];
}
document.write(result);
</script>
</body>
</html>
8.9生成随机颜色值
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
function getRandomColor()
{
var r = Math.floor(Math.random() * (255 + 1));
var g = Math.floor(Math.random() * (255 + 1));
var b = Math.floor(Math.random() * (255 + 1));
var rgb = "rgb(" + r + "," + g +"," + b + ")";
return rgb;
}
document.write(getRandomColor());
</script>
</head>
<body>
</body>
</html>