如何生成伪随机数(最好是Lua),其中生成器给出小数的概率较高?
在我的情况下,我想在游戏中给出一个随机分数,其中获得较低分数是常见的,但较高分数似乎很少。 我见过使用表的加权随机数生成器,但它不符合我的计划。我只想指定最小值(0)最大值(变量)并确保大多数数字保持低位。
我确信通过简单的数学运算可以实现这一点,但我不记得它是哪一个。喜欢过滤math.random的常规输出,不需要真正随机的生成器。
答案 0 :(得分:13)
试试math.floor(minscore+(maxscore-minscore)*math.random()^2)
。调整功率以适合您所需的分布。
答案 1 :(得分:9)
我发现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,则结果将是一般随机性。
示例(全部有100万次迭代,min = 1,max = 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: 40123 (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%)
答案 2 :(得分:4)
我只是转换标准随机函数的值,如下所示:
r1=math.random(0,255)
r2=math.exp(math.random(0,255))
你需要考虑你的界限,但你会得到很多低值的东西,而且很少有更高的值。
答案 3 :(得分:2)
这可能不是你想要的,因为它不是一个平滑的偏差钟形曲线,但为什么不创建两个步骤?定义获得较低范围分数的概率,如果匹配,则范围是较低范围。否则,您的范围是从低范围的顶部到高范围的结束。
净效果是你通常会得到一个低分,但有时候你得分很高。我打赌它会看起来很不错,而且很简单。
您怎么看?
答案 4 :(得分:0)
$ npm i --D webpack@5.24.0 webpack-cli webpack-server html-webpack-plugin babel-loader webpack-dev-server
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: netlify-foot@0.1.0
npm ERR! Found: react@17.0.2
npm ERR! node_modules/react
npm ERR! react@"^17.0.2" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"~0.9" from webpack-server@0.1.2
npm ERR! node_modules/webpack-server
npm ERR! dev webpack-server@"*" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR!
npm ERR! See C:\AppData\Local\npm-cache\eresolve-report.txt for a full report.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\AppData\Local\npm-cache\_logs\2021-06-05T15_25_05_470Z-debug.log
如何调用:
{
"dependencies": {
"@testing-library/jest-dom": "^5.12.0",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.2"
}
}