Hi guys, i’ve made this rarity system, it hasn’t any issue, but i wonder if is there a way to better implement the luck system, or just all the system to be more accurate, here’s the modules:
local module = {}
function module.chooseIndex(rarityTable, luck)
local newRarityArray = {}
local totalWeight = 0
for index, rarity in pairs(rarityTable) do
local weight = rarity[1]
-- it decrease weight only for high rarity
local newWeight = weight
if weight > 100 then -- For example consider legendary of higher
newWeight = weight / (1 + luck)^2
end
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 = "Common"
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
return module
Here’s the rarity table with the weights:
return{
["Common"] = {2},
["Uncommon"] = {5},
["Rare"] = {10},
["Epic"] = {50},
["Legendary"] = {100},
["Rainbow"] = {250},
["Variant"] = {10000},
["Mythic"] = {500},
["Celestial"] = {1000},
["Arcane"] = {5000},
["Divine"] = {10000},
["Ethereal"] = {50000},
["Supernatural"] = {100000},
["Cosmic"] = {500000},
["Titanic"] = {1000000},
["Primordial"] = {2000000},
["Transcendent"] = {5000000},
["GodLike"] = {10000000}
}