Dicebag - probability for games

M.bag_draw(id) function , when the quantity is exhausted and not reset, it leads to the ‘interval is empty’ error.
I fixed it like this.

function M.bag_draw(id)
    if not M.bags[id] then
        return false
    end

    if M.bags[id].success + M.bags[id].fail < 1 then
        M.bag_reset(id)
    end

    local result = math.random(1, M.bags[id].success + M.bags[id].fail)
    ...
2 Likes