Math.random()
Math.random()
返回 0(含)和 1 之间的随机数 (独家的):
// Returns a random number:
Math.random();
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.random()</h2>
<p>Math.random() returns a random number between 0 (included) and 1 (excluded):</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.random();
</script>
</body>
</html>
Math.random()
始终返回小于 1 的数字。
与 Math.floor()
一起使用的 Math.random()
可用于返回随机整数。
JavaScript 中不存在整数这样的东西。
我们在这里讨论的是没有小数的数字。
// Returns a random integer from 0 to 9:
Math.floor(Math.random() * 10);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math</h2>
<p>Math.floor(Math.random() * 10) returns a random integer between 0 and 9 (both
included):</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
Math.floor(Math.random() * 10);
</script>
</body>
</html>
// Returns a random integer from 0 to 10:
Math.floor(Math.random() * 11);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math</h2>
<p>Math.floor(Math.random() * 11) returns a random integer between 0 and 10 (both included):</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
Math.floor(Math.random() * 11);
</script>
</body>
</html>
// Returns a random integer from 0 to 99:
Math.floor(Math.random() * 100);
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math</h2>
<p>Math.floor(Math.random() * 100)) returns a random integer between 0 and 99
(both included):</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
Math.floor(Math.random() * 100);
</script>
</body>
</html>
// Returns a random integer from 0 to 100:
Math.floor(Math.random() * 101);
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math</h2>
<p>Math.floor() used with Math.random() * 101 returns a random integer between 0 and 100
(both included):</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
Math.floor(Math.random() * 101);
</script>
</body>
</html>
// Returns a random integer from 1 to 10:
Math.floor(Math.random() * 10) + 1;
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math</h2>
<p>Math.floor(Math.random() * 10) + 1) returns a random integer between 1 and 10
(both included):</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
Math.floor(Math.random() * 10) + 1;
</script>
</body>
</html>
// Returns a random integer from 1 to 100:
Math.floor(Math.random() * 100) + 1;
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math</h2>
<p>Math.floor(Math.random() * 100) + 1) returns a random integer between 1 and
100 (both included):</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
Math.floor(Math.random() * 100) + 1;
</script>
</body>
</html>
正如您从上面的示例中看到的,创建适当的随机函数可能是个好主意 用于所有随机整数目的。
此 JavaScript 函数始终返回 min(包括)和 min 之间的随机数 最大(不包括):
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min) ) + min;
}
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.random()</h2>
<p>Every time you click the button, getRndInteger(min, max) returns a random number between 0
and 9 (both included):</p>
<button onclick="document.getElementById('demo').innerHTML = getRndInteger(0,10)">Click Me</button>
<p id="demo"></p>
<script>
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
</script>
</body>
</html>
这个 JavaScript 函数总是返回一个介于 min 和 max 之间的随机数(包括两者):
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.random()</h2>
<p>Every time you click the button, getRndInteger(min, max) returns a random number between 1 and 10 (both included):</p>
<button onclick="document.getElementById('demo').innerHTML = getRndInteger(1,10)">Click Me</button>
<p id="demo"></p>
<script>
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
</script>
</body>
</html>