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
local chances = {
    ['variable1'] = 70, 
    ['variable2'] = 20,
    ['variable3'] = 10, 
    -- [variable] = chance integer 
}

local results = {}

table.foreach(chances, function(key, value)
    for i = 1, value do
        table.insert(results, key)
        print(key)
    end
end)

local random = math.random(#results)
print(random)
print(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