[英]Math.random in a Functions parameters where the function is held within a variable - answer always the same
[英]Math.Random in function always the same result
我有這個代碼:
function raffle(){
number = Math.random(100) * 100;}
raffle();
但每次我抽獎(); 號碼是一樣的。
Math.random()
返回一個介於 0(含)和 1(不含)之間的隨機數。 Javascript random
函數不接受任何參數。
如果你想要一個隨機數 x 使得 0 ≤ x < 100,那么你可以這樣做:
function raffle() {
return Math.random() * 100;
}
您的抽獎功能永遠不會返回值。
這是一個返回隨機值的版本。
function raffle() {
return Math.random() * 100;
}
(Math.random() 函數返回 [0, 1) 范圍內的浮點偽隨機數,即從 0(包括)到但不包括 1(不包括),然后您可以縮放到你想要的范圍。)
您可以簡單地將迭代次數添加到每個結果中,如下所示:
let runNumber = 0
function raffle(){
return Math.random() * 100 + runNumber++;
}
raffle();
你可能一個接一個地運行它,這就是“偽”隨機部分開始的地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.