JavaScript? ?? ??
Math ?? ??
Math ??? ?? ?????. ?, Math ??? ??? ? Math ??? ??? ??? ????. ????? ????.
Math.PI: ??.
Math.abs(): ???. ?: Math.abs(-9) = 9
Math.ceil(): ??????(??? 1? ??? ??? ??). ?: Math.ceil(10.2) = 11
Math.floor(): ???(??? ?? ??). ?: Math.floor(9.888) = 9
Math.round(): ???. ?: Math.round(4.5) = 5; Math.round(4.1) = 4
Math.pow(x,y): x? y??? ????. ?: Math.pow(2,3) = 8
Math.sqrt(): ???? ????. ?: Math.sqrt(121) = 11
Math.random(): 0? 1 ??? ??? ???? ?????. ?: Math.random() = 0.12204467732259783
??: (??, ??) ???? ??? ??? ????. ??? ??? ????. Math.random()*(max-min)+min
?: 0-10 ??? ??? ?? 10-20 ??? ??? ?? ??; 20?? 30 ??? ??? ??? ????. 7?? 91 ??? ??? ??? ????.
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> //求兩個整數(shù)之間的隨機整數(shù) //定義隨機數(shù)的函數(shù) function getRandom(min,max){ //求隨機數(shù) var random =Math.random()*(max-min)+min; //向下取整 random = Math.floor(random); //輸出結果 document.write(random+"<hr>"); } //調用函數(shù) getRandom(0,100); getRandom(5,89); getRandom(100,999); </script> </head> <body> </body> </html>
?: ??? ? ??? ?? ??
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> </head> <body> </body> </html> <script> var min = 100000; var max = 999999; var random = Math.random() *(max-min)+min; //向下取整 random = Math.floor(random); document.body.bgColor = "#"+random; </script>