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

Getting a random key from a dictionary table?

Asked by 5 years ago
Edited 5 years ago

I'm attempting to solve one of my previous queries and in order to have a breakthrough all I need to know is if it's possible to search through an entire table and store a random key within a variable while ignoring it's number values.

For example using this table I would like to randomly output one of the dictionary names such as Apples or Cabbages (quotations if possible) but NOT its number values.

Is this possible with tables or should I use a seperate modified table or array?

ExampleTable = {
["Apples"] = 0,
["Berries"] = 32,
["Cabbages"] = 5,
["Daikons"] = 89,
["Eggs"] = 9
}

RandomKeySelector =

print("The random food item is",RandomKeySelector)

2 answers

Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
5 years ago
Edited 5 years ago

For get a Random key from dictionary table you need to create a array table with index of all elements of your dictionary table, For example:

local dictionary = {
    ["Eggs"] = 10,
    ["Apples"] = 50
}

local function GenerateRandom()
    --> You need to create a fake array table.
    local Array = {}

    for index in pairs(dictionary) do
        table.insert(Array, index) --> Insert the index to array table
    end

    --> After that only get the random number from array table.
    local RandomNumber = math.random(1, #Array) --> Will get a random number of array

    return Array[RandomNumber], dictionary[Array[RandomNumber]] --> Return the name and value after that
end

local Name, Value = GenerateRandom()

print(Name, Value)

Basically, i created a fake array table, added the index of the dictionary table, after that i generated the random value.

Fixed script:

local ExampleTable = {
    ["Apples"] = 0,
    ["Berries"] = 32,
    ["Cabbages"] = 5,
    ["Daikons"] = 89,
    ["Eggs"] = 9
}

local function GenerateRandomIndex()
    local Array = {}

    for index in pairs(ExampleTable) do
        table.insert(Array, index)
    end

    local RandomNumber = math.random(1, #Array)

    return Array[RandomNumber], ExampleTable[Array[RandomNumber]] 
end

local ObjName, ObjValue = GenerateRandomIndex()

print("The random food item is", ObjName, "The value is", ObjValue)

Hope it helped!

0
Too much work. Too bloated. Could have all been done in a single function call. User#24403 69 — 5y
0
Edited. yHasteeD 1819 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

You want to use math.random(). Set the first argument to 1 and the next to #ExampleTable. The hashtag means it will get the length. If there were 5 items in the table, it would be 1, 5. But from my understanding of tables like this in the past, I had to put another table inside of the table because you can't get the name if the key has brackets around it.

ExampleTable = {
{Name="Apples",Number=0},
{Name="Barries",Number=32},
{Name="Cabbages",Number=5},
{Name="Diakons",Number=89},
{Name="Eggs",Number=9}
}
print(#ExampleTable)
local numberPosition = math.random(1, #ExampleTable)
RandomKeySelector = ExampleTable[numberPosition].Name

print("The random food item is ",RandomKeySelector)
0
A dictionary table will always return 0 when you try to get the length. That's why I removed it. namespace25 594 — 5y

Answer this question