local chances = {100, 50, 2}
-- add all the chances up into a total value this will be 152 in this example
local total = 0
for i, value in ipairs(chances) do total += value end
-- we pick a random number between 0 and 152
-- then we loop each chance value and deduct it from the random value
-- once the random value goes below or equal to 0 that is the item that was selected
local function GetRandom()
local randomValue = math.random() * total
for i, value in ipairs(chances) do
randomValue -= value
if randomValue <= 0 then return i end
end
end
-- 1 has a 65.7% chance (100 / 152 * 100 = 65.78947368421053)
-- 2 has a 32.8% chance (50 / 152 * 100 = 32.89473684210526)
-- 3 has a 1.3% chance (2 / 152 * 100 = 1.315789473684211)
print(GetRandom())
print(GetRandom())
print(GetRandom())
print(GetRandom())
print(GetRandom())