Is there a way to generate something randomly with one outcome being more probable than the other? For example, could I have one map having an 80% chance of happening, another map having a 19% chance of loading, and another one having a 1% chance of loading?
01 | local chances = { |
02 | [ 'variable1' ] = 70 , |
03 | [ 'variable2' ] = 20 , |
04 | [ 'variable3' ] = 10 , |
05 | -- [variable] = chance integer |
06 | } |
07 |
08 | local results = { } |
09 |
10 | table.foreach(chances, function (key, value) |
11 | for i = 1 , value do |
12 | table.insert(results, key) |
13 | print (key) |
14 | end |
15 | end ) |
16 |
17 | local random = math.random(#results) |
18 | print (random) |
19 | print (results [ random ] ) |