zbizu
Legendary OT User
this function chooses one random element based on their chance to appear
input:
output:
function:
input:
Code:
-- chances must be integers!
selectByChance({element1, element2, ...}, {chance1, chance2, ...})
output:
Code:
elementx
function:
Code:
function selectByChance(elements, chances)
if #elements ~= #chances then
return elements[math.random(1, #elements)]
end
local maxChance = 0
for i = 1, #chances do
maxChance = maxChance + chances[i]
end
local roll = math.random(1, maxChance)
local chance = 0
for i = 1, #elements do
chance = chance + chances[i]
if roll <= chance then
return elements[i]
end
end
end