What is wrong in this rng script?

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)

your confusion here probably stems from your expectations. if u need exact percentages, define weights with a consistent base number

local PrizeTable = {
	["Rarity1"] = 100000,
	["Rarity2"] = 50000,
	["Rarity3"] = 10000,
	["Rarity4"] = 1000,
	["Rarity5"] = 100,
	["Rarity6"] = 10,
	["Rarity7"] = 1,
}

You’re converting weights like 1 in 10 (10%) into probabilities and normalizing them relative to the sum of all probabilities.

This is your table yeah?

["Rarity1"] = 2        -- 1 in 1 → 50%
["Rarity2"] = 4        -- 1 in 2 → 25%
["Rarity3"] = 10       -- 1 in 10 → 10%
["Rarity4"] = 100      -- 1 in 100 → 1%

But you’re only doing the following:
Weight = 1 / chance

So instead of true independent probabilities (like rolling a die with known 1-in-X odds), you’re saying “out of everything, Rarity3 gets 1/10 of the weight” relative to all other weights — and that’s not the same as a literal 10% chance.