2010-12-12 88 views
9

如何生成一個僞隨機數(最好在Lua中),其中發生器給出小數字的概率較高?隨機數發生器具有較高的低值概率?

在我的情況下,我想給一個隨機分數在一個遊戲中獲得較低分數的常見情況,但較高分數很少出現。 我見過使用表格的加權隨機數生成器,但它不符合我的計劃。我只想指定最小值(0)的最大值(變量),並確保大部分數字保持較低。

我相信這是可能的一個簡單的數學運算,但我不記得它是哪一個。就像過濾math.random的常規輸出一樣,不需要真正的隨機生成器。

回答

2

這可能不是你要找的,因爲它不是一個平穩有偏見的鐘形曲線,但爲什麼不創建兩個步驟?定義獲得較低範圍分數的概率,如果匹配,您的範圍就是較低範圍。否則,您的範圍是從低範圍的頂部到高範圍的結尾。

最終結果是,你通常會得到一個低分,但有時你會得到高分。我敢打賭它看起來不錯,而且非常簡單。

您認爲如何?

+0

呵呵,其實是能正常工作,非常感謝你! – viktorry 2010-12-12 05:36:32

4

我只是將標準隨機函數的值,如:

r1=math.random(0,255) 
r2=math.exp(math.random(0,255)) 

你需要考慮到你的範圍,但你必須用的東西很多低值,還有幾個更高的。

13

嘗試math.floor(minscore+(maxscore-minscore)*math.random()^2)。調整功率以適合您所需的分配。

7

我發現Ihf's answer非常有用的,並創建一個C#方法爲它:

private int GetRandomNumber(int max, int min, double probabilityPower = 2) 
    { 
     var randomizer = new Random(); 
     var randomDouble = randomizer.NextDouble(); 

     var result = Math.Floor(min + (max + 1 - min) * (Math.Pow(randomDouble, probabilityPower))); 
     return (int) result; 
    } 

如果probabilityPower大於1,較小的值會比更高的值更常見。 如果它在0到1之間,較高的值將比較低的值更常見。 如果它是1,結果將是一般隨機性。

實例(全部用1百萬次迭代,MIN = 1,最大值= 20):


probabilityPower = 1。5

1: 135534 (13.5534%) 
2: 76829 (7.6829%) 
3: 68999 (6.8999%) 
4: 60909 (6.0909%) 
5: 54595 (5.4595%) 
6: 53555 (5.3555%) 
7: 48529 (4.8529%) 
8: 44688 (4.4688%) 
9: 43969 (4.3969%) 
10: 44314 (4.4314%) 
11: 4(4.0123%) 
12: 39920 (3.992%) 
13: 40466 (4.0466%) 
14: 35821 (3.5821%) 
15: 37862 (3.7862%) 
16: 35222 (3.5222%) 
17: 35902 (3.5902%) 
18: 35202 (3.5202%) 
19: 33961 (3.3961%) 
20: 33600 (3.36%) 

probabilityPower = 4

1: 471570 (47.157%) 
2: 90114 (9.0114%) 
3: 60333 (6.0333%) 
4: 46574 (4.6574%) 
5: 38905 (3.8905%) 
6: 32379 (3.2379%) 
7: 28309 (2.8309%) 
8: 27906 (2.7906%) 
9: 22389 (2.2389%) 
10: 21524 (2.1524%) 
11: 19444 (1.9444%) 
12: 19688 (1.9688%) 
13: 18398 (1.8398%) 
14: 16870 (1.687%) 
15: 15517 (1.5517%) 
16: 15871 (1.5871%) 
17: 14550 (1.455%) 
18: 14635 (1.4635%) 
19: 13399 (1.3399%) 
20: 11625 (1.1625%) 

probabilityPower = 1

1: 51534 (5.1534%) 
2: 49239 (4.9239%) 
3: 50955 (5.0955%) 
4: 47992 (4.7992%) 
5: 48971 (4.8971%) 
6: 50456 (5.0456%) 
7: 49282 (4.9282%) 
8: 51344 (5.1344%) 
9: 50841 (5.0841%) 
10: 48548 (4.8548%) 
11: 49294 (4.9294%) 
12: 51795 (5.1795%) 
13: 50583 (5.0583%) 
14: 51020 (5.102%) 
15: 51060 (5.106%) 
16: 48632 (4.8632%) 
17: 48568 (4.8568%) 
18: 50039 (5.0039%) 
19: 49863 (4.9863%) 
20: 49984 (4.9984%) 

probabilityPower = 0.5

1: 3899 (0.3899%) 
2: 5818 (0.5818%) 
3: 12808 (1.2808%) 
4: 17880 (1.788%) 
5: 23109 (2.3109%) 
6: 26469 (2.6469%) 
7: 33435 (3.3435%) 
8: 35243 (3.5243%) 
9: 42276 (4.2276%) 
10: 47235 (4.7235%) 
11: 52907 (5.2907%) 
12: 58107 (5.8107%) 
13: 63719 (6.3719%) 
14: 66266 (6.6266%) 
15: 72708 (7.2708%) 
16: 79257 (7.9257%) 
17: 81830 (8.183%) 
18: 87243 (8.7243%) 
19: 90958 (9.0958%) 
20: 98833 (9.8833%) 

probabilityPower = 0.4

1: 917 (0.0917%) 
2: 3917 (0.3917%) 
3: 3726 (0.3726%) 
4: 10679 (1.0679%) 
5: 13092 (1.3092%) 
6: 17306 (1.7306%) 
7: 22838 (2.2838%) 
8: 29221 (2.9221%) 
9: 35832 (3.5832%) 
10: 38422 (3.8422%) 
11: 47800 (4.78%) 
12: 53431 (5.3431%) 
13: 63791 (6.3791%) 
14: 69460 (6.946%) 
15: 75313 (7.5313%) 
16: 86536 (8.6536%) 
17: 95082 (9.5082%) 
18: 103440 (10.344%) 
19: 110203 (11.0203%) 
20: 118994 (11.8994%)