• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Math.Random high chance to low numbers

zxmatzx

Advanced OT User
Joined
Dec 1, 2010
Messages
311
Solutions
27
Reaction score
154
Location
Brazil
GitHub
Mateuso8
Hi folks, i'm needing help with a function. I need a function that receive 2 integers numbers and return a random number between them with bigger chance to get small numbers instead big numbers. Could work with percents interval?

I've found on internet some ideas, like random weighted numbers generator, i had some troubles trying adapt the code to my necessitys.
I would like understand how this code work, i'll keep trying, but would be nice recive anothers approach to do that.

Thanks.
 
Solution
Just did a quick search on stackoverflow and found this:
Random number generator with higher probabilities of giving low values? (https://stackoverflow.com/questions/4420502/random-number-generator-with-higher-probabilities-of-giving-low-values/4421297#4421297)
You can actually create a function with it.
Lua:
function getRandomNumber(max, min, probabilityPower)
    return math.floor(min + (max + 1 - min) * (math.random() ^ probabilityPower))
end
quoting the author message:
If probabilityPower is above 1, lower values will be more common than higher values. If it's between 0 to 1, higher values will be more common than lower values. If it's 1, the results will be in a general randomness.

Examples (all with 1 million...
Just did a quick search on stackoverflow and found this:
Random number generator with higher probabilities of giving low values? (https://stackoverflow.com/questions/4420502/random-number-generator-with-higher-probabilities-of-giving-low-values/4421297#4421297)
You can actually create a function with it.
Lua:
function getRandomNumber(max, min, probabilityPower)
    return math.floor(min + (max + 1 - min) * (math.random() ^ probabilityPower))
end
quoting the author message:
If probabilityPower is above 1, lower values will be more common than higher values. If it's between 0 to 1, higher values will be more common than lower values. If it's 1, the results will be in a general randomness.

Examples (all with 1 million iterations, min = 1, max = 20 ):
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%)

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%)

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%)
 
Solution
Just did a quick search on stackoverflow and found this:
Random number generator with higher probabilities of giving low values? (https://stackoverflow.com/questions/4420502/random-number-generator-with-higher-probabilities-of-giving-low-values/4421297#4421297)
You can actually create a function with it.
Lua:
function getRandomNumber(max, min, probabilityPower)
    return math.floor(min + (max + 1 - min) * (math.random() ^ probabilityPower))
end
quoting the author message:
Thanks for help, i found this way when i was searching too. Already looked to the formula with a math perspective too. Just seeking others ways to do the same thing xD
I send the problem to a math teacher friend, he told to do:
"Do a algorithm to get 100 numbers to
represent 30% first numbers, 50 numbers to 50% and 10 numbers to 20% last numbers, then get a random number in all these 160 numbers and u will have more chance to get a lower-middle number"
I don't know how method i will use for my code, just looking and geting info.
Thanks a lot for help.

PS: Your answer can be the best answer, but how i said, im looking many ways to do the same thing. I have to close thread now?
 
Back
Top