[英]Math.Random the highest value
<!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 (included) and 10 (excluded):</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>
上面來自W3Schools的代碼表示該函數返回 0(包含)和 10(排除)之間的某個值。 由於 Math.Random 的值是從 0 到 1,我認為它可以返回 10 個值,只是這種可能性非常小。 它可能是1 * (10 - 0) + 0 = 10
並且 Math.floor 在這里沒有任何作用。 我是不是不對,真的排除了 10 個值?
Math.random() 函數返回范圍為 0 到小於 1(包括 0,但不包括 1)的浮點偽隨機數,在該范圍內分布大致均勻 - 然后您可以縮放到您想要的范圍。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.