• Retrieve random key from a table
    4 replies, posted
Is there any way to retrieve a random key from a table, [B]without[/B] using 2 sets of tables/arrays? So if I have a table:[CODE]tbl = { First = 10, Second = 382, Third = "banana", }[/CODE] I want to randomly get First, Second, or Third
table.Random?
Not sure if there's a direct way to do this but you could always do [lua] table.Random( table.GetKeys( tabl ) ) [/lua] But that will give you a random key ("First", "Second" or "third") so make sure that's what you want, otherwise just use table.Random
local _, key = table.Random( tbl)
[QUOTE=James xX;48828316]Not sure if there's a direct way to do this but you could always do [lua] table.Random( table.GetKeys( tabl ) ) [/lua] But that will give you a random key ("First", "Second" or "third") so make sure that's what you want, otherwise just use table.Random[/QUOTE] Thanks
Sorry, you need to Log In to post a reply to this thread.