JavaScript 随机数
js随机小数
Math.random()
Math.random() //返回 0(包括) 至 1(不包括) 之间的随机数:
Math.random() 总是返回小于 1 的数。
JavaScript 随机整数
Math.floor(Math.random() * 11); // 返回 0 至 10 之间的数
目前最常用的方法
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.random()</h2>
<p>每当您点击按钮,getRndInteger(min, max) 就会返回 0 与 9(均包含)之间的随机数:</p>
<button onclick="document.getElementById('demo').innerHTML = getRndInteger(0,10)">点击我</button>
<p id="demo"></p>
<script>
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
</script>
</body>
</html>
- 是否有用?
如果有用请点赞