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

Efficient way to return something by chance?

Asked by 5 years ago

Aloha, existent ones. I want to make a key-value dictionary where the key is a thing (string, number, etc.) and the value is the chance of it being returned. I can't supply any code (sorry) because I'm not too sure how to go about doing this.

Does anyone have a way to do this?

0
If you want the value to be the chance of it being returned, that is just to complex and inefficient. I recommend that you change your code structure so you don't need to do that. User#25115 0 — 5y
0
No problem. Glad I could help! User#25115 0 — 5y

1 answer

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

Hello there! This is not too difficult/complex to do. The simplest method I could think of, was 1.) get the length of the dictionary, 2.) randomly choose a number in that length, 3.) loop until you reach that random number. Because the iterator, pairs, is arbitrary already, this should leave you with a pretty random selection. You cannot just get the length of a dictionary with the # operator in Lua. You would have to create your own method. I recently posted three different methods discussed here. Ok, so now to the example:

local function dictLengthFinder(tbl) -- using one of the three methods
    local length = 0
    for i,v in pairs(tbl) do
        length = length + 1
    end
    return length
end
local function getRan(dict)
    local dictLength = dictLengthFinder(dict)
    local ranIndex = math.random(dictLength)
    local counter = 1
    for i,v in pairs(dict) do
        if counter ~= ranIndex then
            counter = counter + 1
        else
            return v 
            --[[
                I don't know what you want here. 
                I will just return the value. 
                You could also return i if you wanted.
            --]]
        end
    end
end
local dict = {} -- some dictionary
local ranItem = getRan(dict)

I hope this helps. Have a great day scripting!

Edit:

For the value to determine the probability would be rather inefficient, and there are probably better ways you could structure your code in order to remove the need for this. I will, however, walk you through how to accomplish this. 1.) create a table named chanceTbl (for this example) 2.) loop through your dictionary and store the index value amount of times in the chanceTbl 3.) randomly select an index in the chanceTbl and return the value, which would be the index in your dictionary. Here is an example:

local function getRan(dict)
    local chanceTbl = {}
    for i,v in pairs(dict) do
        for num = 1, v do
            table.insert(chanceTbl, i)
        end
    end
    local ranIndex = math.random(#chanceTbl)
    --[[
        we are able to get the length of chanceTbl with # because all the   
        indices are in numeric order because we used table.insert
    --]]
    local ranItem = chanceTbl[ranIndex] 
    --[[
        I used two different variables so that you could 
        clearly see what is going on. Note that you could 
        do this in one variable if you just defined the variable like this:
        local ranIndex = chanceTbl[math.random(#chanceTbl)] 
    --]]
    return ranItem
    --[[
        In fact, you could just return the whole statement:
        return chanceTbl[math.random(#chanceTbl)]   
    --]]
end
local dict = {} -- some dictionary
local ranItem = getRan(dict)

Good luck!

0
The code is pretty self explanatory, but if you don't understand anything, feel free to comment below. User#25115 0 — 5y
0
The second example assumes that you will be inputting probability in whole numbers. User#25115 0 — 5y
0
For example: ["blue"] = 5, ["brown"] = 16, ["black"] = 30, ["bronze"] = 43. User#25115 0 — 5y
0
Note: Those are not percentages. They are just the amount that will appear in the chanceTbl. User#25115 0 — 5y
0
It seemed to work fine, thanks. Fireorius 51 — 5y
Ad

Answer this question