源代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>享学课堂</title> </head> <body> <h1>JavaScript Math.random()</h1> <p>每次你点击按钮,getRndInteger(min,max)返回一个0的随机数(包括)和10(排除):</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>
运行结果