如何在JavaScript中生成两个数字之间的随机数?
若要生成两个数字之间的随机数,请使用Math.random()方法。
示例
您可以尝试运行以下代码以获取介于最小值和最大值之间的随机数-
<!DOCTYPE html>
<html>
<body>
<script>
function randomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
alert(randomInt(5,25));
</script>
</body>
</html>