我製作了舊版風險棋盤遊戲。我使用java.util.Random
來實施軍隊互相攻擊時的擲骰。例如,當3名軍隊攻擊領土兩軍,我一定要「滾」 5模具總數:Random vs SecureRandom在遊戲中
Attacker rolls: 3 die
Defender rolls: 2 die
int[] rollsAttacker = new int[3];
int[] rollsDefender = new int[2];
for (int i = 0; i < 3; i++) {
rollsAttacker[i] = mRandom.nextInt(6) + 1;
}
for (int i = 0; i < 2; i++) {
rollsDefender[i] = mRandom.nextInt(6) + 1;
}
// compare rolls and figure out who won...
// ...
我得到一個非常高的投訴,有太多的「冷門」。例如,一支100人的軍隊可能會輸給一支2人的軍隊。我已經運行了數百萬次的滾動方法來測試贏/輸比率,並且他們幾乎完全匹配做同樣事情的其他站點。
我在考慮切換到SecureRandom,它可能是「更隨機」,但不知道它是否值得。我不知道util.Random可能會如此頻繁地產生這樣的干擾。用戶可以在移動設備上玩遊戲,那麼util.Random的實現可能會在某些平臺上產生較少的差異? (投訴似乎來自特定品牌的手機)。
因此,在短期,我不知道如果在切換到SecureRandom的會在我的情況有所幫助,如果它要慢得多,或者如果我只是沒有使用正確util.Random,
----- ------更新----------------
防禦者獲勝的4對1攻擊的例子。
Attacking 4 vs 1...
Roll:
a: 5, 5, 4
d: 6
Roll:
a: 5, 2
d: 5
Roll:
a: 3
d: 5
Defender survived!
我試過在上述4對1攻擊中運行我的攻擊方法分批運行20,000次,攻擊者贏得91%的時間。這比97%的勝率推出維基百科頁面上的這個遊戲要低得多:
哎呀。
------更新(再次)-------------
其實,我一直在尋找圖表一個插槽後面不小心,我的號碼排隊什麼圖表有:
0.4139 0.7502 0.9132 0.97245 0.99015 0.9964 0.9991 0.99975 0.9999 1.0
0.10865 0.36105 0.649 0.78555 0.8924 0.9343 0.9691 0.98 0.9904 0.9951
0.02655 0.2065 0.4729 0.6414 0.76725 0.8584 0.9121 0.94605 0.9687 0.982
0.00665 0.0905 0.3182 0.48215 0.63985 0.7432 0.8304 0.8894 0.9274 0.9525
0.0015 0.04775 0.20575 0.35955 0.50885 0.64125 0.7389 0.8168 0.8753 0.9164
5.5E-4 0.0229 0.1323 0.25105 0.39385 0.52695 0.6436 0.7254 0.805 0.86415
0.0 0.00975 0.0854 0.18075 0.29985 0.42645 0.5389 0.64695 0.72615 0.8009
5.0E-5 0.00545 0.05515 0.1255 0.2228 0.3307 0.4438 0.54365 0.64505 0.723
0.0 0.00255 0.03225 0.0877 0.16005 0.2576 0.3519 0.46685 0.559 0.6445
0.0 0.00125 0.0201 0.0569 0.11595 0.19315 0.2898 0.374 0.47505 0.56275
所以是啊不知道我可以告訴用戶除了可能會發生翻倒。
然後,如何根據所有這些捲來決定獲勝者?也許這是一個邏輯缺陷。 – 2012-04-24 16:55:59
你的邏輯似乎比「隨機」不是「足夠隨機」更有問題。 – 2012-04-24 16:59:37