#Pls may someone help (Weighted RNG)

1 messages · Page 1 of 1 (latest)

last pewter
#

Because my script is:

local luckMulti = 1

local elements = {
    {name = "Water", weight = 1/2},
    {name = "Fire", weight = 1/3},
    {name = "Air", weight = 1/6},
}

local function rollElement()
    local totalWeight = 0
    for _, element in ipairs(elements) do
        totalWeight += element.weight
    end

    local randomNumber = math.random() * totalWeight
    local cumulative = 0

    for _, element in ipairs(elements) do
        cumulative += element.weight
        if randomNumber <= cumulative then
            return element.name
        end
    end
end

and ppl told me to add weight

I've been told to do weight/(weightMulti*luckmulti) and make the weight Multiple a number 0 (common) - 1 (rarest) but I don't understand how tf to implement it

#

up

slim spear
slim spear
#

should you not do this?

local elements = {
    {name = "Water", weight = 2},
    {name = "Fire", weight = 3},
    {name = "Air", weight = 6},
}```