实例代码

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />

<title>js获取随机数</title>

</head>

<body>

ceil:<br />

<input type="text" id="ceil" /><br />

round:<br />

<input type="text" id="round" /><br />

floor:<br />

<input type="text" id="floor" /><br />

round2:<br />

<input type="text" id="round2" /><br />

<button onclick="clickMath()">生成随机数</button>

<script>

function clickMath(){

var ceil = Math.ceil(Math.random()*10);     

document.getElementById("ceil").value = ceil;

var round = Math.round(Math.random());       

document.getElementById("round").value = round;

var floor= Math.floor(Math.random()*10); 

document.getElementById("floor").value = floor;

var round2 = Math.round(Math.random()*10); 

document.getElementById("round2").value = round2;

}

</script>

</body>

</html>