Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I get this tables size to change based on the percentages I put in?

Asked by 5 years ago

Here is my script:

    local lPC = 1 -- what if I changed these to decimals. That is where I want the table to become bigger so that a value will be put into the table, but be the percent chance that I want.
    local rPC = 9
    local uPC = 25
    local oPC = 65 -- just the chances that it will appear. 

    -- Arrays --

    local boxTable = {}

    -- Functions --

    local function fillBoxTable()

        local totalLengthOfTable = lPC + rPC + uPC + oPC
        local lAmount = math.floor((lPC / 100) * totalLengthOfTable)
        local rAmount = math.floor((rPC / 100) * totalLengthOfTable)
        local uAmount = math.floor((uPC / 100) * totalLengthOfTable)
        local oAmount = math.floor((oPC / 100) * totalLengthOfTable)

        for i = 1, totalLengthOfTable do

            if i <= lAmount then

                boxTable[i] = "Legendary Box"

            elseif i <= rAmount then

                boxTable[i] = "Rare Box"

            elseif i <= uAmount then        

                boxTable[i] = "Uncommon Box"

            elseif i <= oAmount then

                boxTable[i] = "Ordinary Box"

            end

        end

    end

I think the problem is that this will not make a larger table based on percentage sizes. I am really confused and hope someone can help me out. Thanks!

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

I'm guessing you're trying to make a random percent chance for spawning different grades of boxes.

First let's just point out a logic error in your code,

local uPC = 25                              -- Guessing 25% chance

local totalLengthOfTable = lPC + rPC + uPC + oPC    -- It's equal to 100

local uAmount = math.floor((uPC / 100) * totalLengthOfTable)    -- Logic error

Translating your equation into words you're doing 25/100 = .25. Okay, now you're doing .25 * 100, that equals 25 again. What was the point of that?

Here is a way to do percentage chances:

local lengendary = 0.01 -- 1%
local rare = 0.1        -- 10%
local uncommon = 0.25   -- 25%
local common = "whatever doesn't matter"    

local tablesize = 250

local chance = math.random(1,tablesize)

if chance <= legendary*tablesize then
    print'legendary'
elseif chance >= legendary*tablesize and chance <= rare*tablesize + legendary*tablesize then
    print'rare'
elseif chance >= rare*tablesize and chance <= uncommon*tablesize + rare*tablesize then
    print'uncommon'
else
    print'common'
end 

With that way, there is a chance that no legendaries could ever spawn, so if you want to control the values you would just do:

local lengendary = 0.01 -- 1%
local rare = 0.1        -- 9%
local uncommon = 0.25   -- 25%
local common = 0.65 -- 65%

local tablesize = 250

local lNum = math.floor(tablesize * legendary)
local rNum = math.floor(tablesize * rare)
local uNum = math.floor(tablesize * uncommon)
local cNum = math.floor(tablesize * common)

for i = 1, lNum do
    boxtable[i] = "Legendary"
end

for i = lNum+1, rNum do
    boxtable[i] = "Rare"
end

for i = rNum+1, uNum do
    boxtable[i] = "Uncommon"
end

for i = uNum+1, cNum do
    boxtable[i] = "Common"
end

-- Fill in any values that are not filled since we used math.floor
for i,v in pairs(boxtable) do
    if not v or v == "" them
        v  = "Common"
    end
end
0
Thanks for your help! User#21908 42 — 5y
0
While not exactly what I was looking for it still presented some solutions. User#21908 42 — 5y
Ad

Answer this question