#Rarity system

1 messages · Page 1 of 1 (latest)

novel tiger
#

Can someone help me understand as to why this works, I can't seem to grasp it producing accurate results in all samples.

Script:

local moduleTable = require(script.ModuleScript)

for i=1, 1000 do
print(moduleTable.chooseRarity())
end

print(moduleTable.counter)

ModuleScript:

local module = {}

module.counter = {
["Legendary"] = 0,
["Epic"] = 0,
["Rare"] = 0,
["Common"] = 0

}

module.rarities = { -- 
    ["Legendary"] = 0.5,
    ["Epic"] = 9.5,
    ["Rare"] = 30,
    ["Common"] = 60, 

}

module.chooseRarity = function()


local randomNum = (math.random(1, 1000))*0.1
    print(randomNum)

    local counter = 0    


    for rarity, weight in module.rarities do
        
        counter += weight
        
        print("Rarity is ", rarity)
        
        print("Weight is ", weight)
        
        print("Counter is currently: ", counter)
        
        if randomNum <= counter then
            print(rarity, " Has been decided")
            module.counter[rarity] += 1
            return rarity
        end
    end

end

return module

#

For example, if the randomNum ends up being 1, wouldn't it just choose out of epic rare or common all as equal chances? Ín that case it removes the possibility of it being legendary, but still..

#

Yet it always produces results of legendary appearing ~5 times, epic ~95 times, rare ~300 times, ~600 times

floral rose
#

randomNum range is within 0.1 - 100

#

try use Random.new() api

#

so the algorithm iterate through each rarity with its corresponding weight

#

the counter accumulates these weights as it progresses through the rarities

#

when the random number is less than or equal to this accumulated value, the corresponding rarity is chosen

novel tiger
#

itll just choose whichever it lands on first

#

or wait