I wanna achieve the rates like in the table but i cant achive it i tested to roll it bunch of time but for example rarity3 which is 10 weight which should be %10 is not actually %10 should i just set numbers from 1 to 100000 then divide it by 100 and calculate rates like that?
local PrizeTable = {
["Rarity1"] = 2, -- 1 in 1 (100%)
["Rarity2"] = 4, -- 1 in 2 (50%)
["Rarity3"] = 10, -- 1 in 10 (10%)
["Rarity4"] = 100, -- 1 in 100 (1%)
["Rarity5"] = 1000, -- 1 in 1,000 (0.1%)
["Rarity6"] = 10000, -- 1 in 10,000 (0.01%)
["Rarity7"] = 100000, -- 1 in 100,000 (0.001%)
}
local function SetupProbabilities(prizeTable)
local total = 0
for _, chance in pairs(prizeTable) do
total = total + (1 / chance)
end
local probabilities = {}
for prize, chance in pairs(prizeTable) do
table.insert(probabilities, {
Name = prize,
Probability = (1 / chance) / total
})
end
table.sort(probabilities, function(a, b)
return a.Probability < b.Probability
end)
return probabilities
end
local probabilities = SetupProbabilities(PrizeTable)
local function Roll()
local roll = math.random()
local cumulative = 0
for _, entry in ipairs(probabilities) do
cumulative = cumulative + entry.Probability
if roll <= cumulative then
print(cumulative)
return entry.Name
end
end
return probabilities[1].Name
end
local TotalPrizes = {}
for i = 1, 1000 do
local prize = Roll()
TotalPrizes[prize] = (TotalPrizes[prize] or 0) + 1
end
print(TotalPrizes)