Here is my table:
local Horses Horses = { Jack = {0,0,1}, Mac = {0,1,0}, Smitty = {1,0,0}, Jerry = {1,1,0}, Alf = {0,1,1}, Jim = {1,0,1} }
I want to print a random value from the table above.
How do I do it?
Here is my table:
local Horses Horses = { Jack = {0,0,1}, Mac = {0,1,0}, Smitty = {1,0,0}, Jerry = {1,1,0}, Alf = {0,1,1}, Jim = {1,0,1} }
I want to print a random value from the table above.
How do I do it?
If you’re expecting “Jack” as an answer, you won’t get it since Jack is the table name, you would need an entry to include “Jack” if that is what you wanted.
The below will count the table entries, and give you a value between 1 and 6 in this example. You didn’t specify if you wanted random of the 1-6, or random of the 3 table entries 
math.randomseed(os.time()) local Horses ={} Horses = { Jack = {0,0,1}, Mac = {0,1,0}, Smitty = {1,0,0}, Jerry = {1,1,0}, Alf = {0,1,1}, Jim = {1,0,1} } local totalEntries = 0 for a,b in pairs(Horses) do print("Horse "..a.." has "..#Horses[a].." values:1:"..Horses[a][1].." 2:"..Horses[a][2].." 3:"..Horses[a][3]) totalEntries=totalEntries+1 end local randomIt = math.random(totalEntries) print( "Random # is:"..randomIt )
Hi,
To more easily manage the entries for randomization, you might consider modeling your table data differently, so as to create a table “array”:
local Horses = { { name="Jack", pos={0,0,1} }, { name="Mac", pos={0,1,0} }, { name="Smitty", pos={1,0,0} } } local ranHorse = Horses[math.random(1, #Horses)] print(ranHorse.name) print(unpack(ranHorse.pos))
-dev
as per the prior two replies, the question needs to be worded more clearly.
btw, both prior replies are really good, just depending on the assumptions made about what your question actually is. (i’d do it as per develephant)
for example, here’s a “ridiculous” (but valid) reading of your question:
since the “values” that are actually tabled are simply 0 and 1, here’s one of those selected randomly:
local result = math.random(0,1)
but not very helpful, right? :D i must have “mis-read” the question. ;)
Somewhat going off @graham07’s idea, you could dump the key names to a table and then pick a random item from that table:
[lua]
local horseNames = {}
for k,v in pairs(Horses) do
horseNames[#horseNames+1] = k
end
print( horseNames[math.random(1,#horseNames)] )
[/lua]
Brent
If you’re expecting “Jack” as an answer, you won’t get it since Jack is the table name, you would need an entry to include “Jack” if that is what you wanted.
The below will count the table entries, and give you a value between 1 and 6 in this example. You didn’t specify if you wanted random of the 1-6, or random of the 3 table entries 
math.randomseed(os.time()) local Horses ={} Horses = { Jack = {0,0,1}, Mac = {0,1,0}, Smitty = {1,0,0}, Jerry = {1,1,0}, Alf = {0,1,1}, Jim = {1,0,1} } local totalEntries = 0 for a,b in pairs(Horses) do print("Horse "..a.." has "..#Horses[a].." values:1:"..Horses[a][1].." 2:"..Horses[a][2].." 3:"..Horses[a][3]) totalEntries=totalEntries+1 end local randomIt = math.random(totalEntries) print( "Random # is:"..randomIt )
Hi,
To more easily manage the entries for randomization, you might consider modeling your table data differently, so as to create a table “array”:
local Horses = { { name="Jack", pos={0,0,1} }, { name="Mac", pos={0,1,0} }, { name="Smitty", pos={1,0,0} } } local ranHorse = Horses[math.random(1, #Horses)] print(ranHorse.name) print(unpack(ranHorse.pos))
-dev
as per the prior two replies, the question needs to be worded more clearly.
btw, both prior replies are really good, just depending on the assumptions made about what your question actually is. (i’d do it as per develephant)
for example, here’s a “ridiculous” (but valid) reading of your question:
since the “values” that are actually tabled are simply 0 and 1, here’s one of those selected randomly:
local result = math.random(0,1)
but not very helpful, right? :D i must have “mis-read” the question. ;)
Somewhat going off @graham07’s idea, you could dump the key names to a table and then pick a random item from that table:
[lua]
local horseNames = {}
for k,v in pairs(Horses) do
horseNames[#horseNames+1] = k
end
print( horseNames[math.random(1,#horseNames)] )
[/lua]
Brent