亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在JavaScript中生成特定范圍內的隨機整數?

在JavaScript中生成特定范圍內的隨機整數?

在JavaScript中生成特定范圍內的隨機整數?
查看完整描述

4 回答

?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

Mozilla Developer Network頁面上有一些示例:


/**

 * Returns a random number between min (inclusive) and max (exclusive)

 */

function getRandomArbitrary(min, max) {

    return Math.random() * (max - min) + min;

}


/**

 * Returns a random integer between min (inclusive) and max (inclusive).

 * The value is no lower than min (or the next integer greater than min

 * if min isn't an integer) and no greater than max (or the next integer

 * lower than max if max isn't an integer).

 * Using Math.round() will give you a non-uniform distribution!

 */

function getRandomInt(min, max) {

    min = Math.ceil(min);

    max = Math.floor(max);

    return Math.floor(Math.random() * (max - min + 1)) + min;

}

這是它背后的邏輯。這是一個簡單的三條規則:


Math.random()返回Number0(包括)和1(不包括)之間的值。所以我們有這樣的間隔:


[0 .................................... 1)

現在,我們想要一個介于min(包含)和max(獨家)之間的數字:


[0 .................................... 1)

[min .................................. max)

我們可以使用它Math.random來獲取[min,max]區間內的通訊記錄。但是,首先我們應該通過min從第二個區間減去一點來解決問題:


[0 .................................... 1)

[min - min ............................ max - min)

這給出了:


[0 .................................... 1)

[0 .................................... max - min)

我們現在可以申請Math.random然后計算通訊員。我們選擇一個隨機數:


                Math.random()

                    |

[0 .................................... 1)

[0 .................................... max - min)

                    |

                    x (what we need)

所以,為了找到x,我們會做:


x = Math.random() * (max - min);

不要忘記添加min回來,以便我們在[min,max]間隔中得到一個數字:


x = Math.random() * (max - min) + min;

這是MDN的第一個功能。第二個,返回一個介于min和之間的整數max。


現在獲取整數,你可以使用round,ceil或floor。


您可以使用Math.round(Math.random() * (max - min)) + min,但這會產生非均勻分布。這兩種,min而max只有大約一半滾動的機會:


min...min+0.5...min+1...min+1.5   ...    max-0.5....max

└───┬───┘└────────┬───────┘└───── ... ─────┘└───┬──┘   ← Math.round()

   min          min+1                          max

與max從區間中排除,它有一個甚至更少的機會,而不是滾動min。


隨著Math.floor(Math.random() * (max - min +1)) + min你有一個完美均勻的分布。


min.... min+1... min+2 ... max-1... max.... max+1 (is excluded from interval)

|        |        |         |        |        |

└───┬───┘└───┬───┘└─── ... ┘└───┬───┘└───┬───┘   ← Math.floor()

   min     min+1               max-1    max

你不能使用ceil()和-1在那個等式中,因為max現在滾動的機會略少,但你也可以滾動(不需要的)min-1結果。


查看完整回答
反對 回復 2019-05-24
?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

var randomnumber = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;


查看完整回答
反對 回復 2019-05-24
?
莫回無

TA貢獻1865條經驗 獲得超7個贊


的Math.random()

Mozilla Developer Network文檔中:

// Returns a random integer between min (include) and max (include)


Math.floor(Math.random() * (max - min + 1)) + min;

有用的例子:


// 0 - 10

Math.floor(Math.random() * 11);


// 1 - 10

Math.floor(Math.random() * 10) + 1;


// 5 - 20

Math.floor(Math.random() * 16) + 5;


// -10 - (-2)

Math.floor(Math.random() * 9) - 10;


查看完整回答
反對 回復 2019-05-24
  • 4 回答
  • 0 關注
  • 1038 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號