代码示例:(标识:eg_js_random_function_2)
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Math.random()</h2>

<p>每当您点击按钮,getRndInteger(min, max) 就会返回 1 与 10(均包含)之间的随机数:</p>

<button onclick="document.getElementById('demo').innerHTML = getRndInteger(1,10)">点击我</button>

<p id="demo"></p>

<script>
function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min + 1) ) + min;
}
</script>

</body>
</html>
运行结果: