Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Generate randomly with probability?

Asked by 7 years ago

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?

0
num = math.random(1,10) if num < 8 then map1 elseif num >=8 then map2 etc etc Vulkarin 581 — 7y

1 answer

Log in to vote
0
Answered by
fuj 49
7 years ago
Edited 7 years ago
01local chances = {
02    ['variable1'] = 70,
03    ['variable2'] = 20,
04    ['variable3'] = 10,
05    -- [variable] = chance integer
06}
07 
08local results = {}
09 
10table.foreach(chances, function(key, value)
11    for i = 1, value do
12        table.insert(results, key)
13        print(key)
14    end
15end)
16 
17local random = math.random(#results)
18print(random)
19print(results[random])
0
Thank you! Sorry for my ignorance on the basic functions of ROBLOX Lua, just getting started! TiredMelon 405 — 7y
Ad

Answer this question