Weighted Luck System not Accurate?

I have a weighted luck system but it’s not accurate when you have 1 or more Luck.
This is 0 Luck:
image

This is 1 Luck:
image

I rolled 100 Rarities both time. As you can see, I shouldn’t have gotten these Rarities. The script subtracts the Luck from the chance. For example 1/10 becomes 1/9. But if the chance is zero or less then that Rarity gets removed.

This is the script:

function rarityModule.chooseIndex(rarityTable, Luck)
	local newRarityArray = {}

	local totalWeight = 0
	
	for index, rarity in pairs(rarityTable) do
		local weight = rarity[2]

		local newWeight = weight - Luck

		if newWeight < 1 then
			continue
		end

		local fraction = (1 / newWeight)
		totalWeight += fraction
		newRarityArray[index] = {fraction}
	end

	local random = Random.new()
	local rnd = random:NextNumber(0, totalWeight)

	local selectedRarity

	local accumulatedWeight = 0

	for index, rarity in pairs(newRarityArray) do
		accumulatedWeight += rarity[1]

		if rnd <= accumulatedWeight then
			selectedRarity = index
			break
		end
	end

	return selectedRarity

end

Help would really be appreciated because I’ve been trying to solve this problem for hours. <3

1 Like