Display random word from tables

m = {"John", "Steve", "Kris", "Ben"} m.name = m f = {"Katie", "Susan", "Patricia", "Elizabeth"} f.name = f

How I can display random word from both of those two tables?

Code below I am using for random word from one table, but I can’t figure out how to merge or…  tables to be able access words from both. I am thinking about function 50/50 but I am new guy here and I need some help.

Any suggestions?

m = {"John", "Steve", "Kris", "Ben"} tableIdx = math.random(#m) tableWord = m[tableIdx] word = display.newText(tableWord, 100, 100, "Helvetica", 24)

I may not have understood your post, but try this code to see one way to do it:

-- -- Try this code EXACTLY -- local names1 = {"John", "Steve", "Kris", "Ben"} local names2 = {"Katie", "Susan", "Patricia", "Elizabeth"} for i = 1, 100 do local first = names1[math.random(#names1)] local second = names2[math.random(#names2)] local merged = first .. " " .. second print( i, merged ) end

While the above code will print random selections from both tables, it won’t be an evenly random distribution over the short course.  i.e. If you only iterate 3 times you may get repeats.

IMHO, a Shuffle Bag is really the way to go here.

Thanks for help and sorry for my bad formulated question.

I want to display one random word at a time from “WORDS” table, but I can’t get access for words in those nested  “M” and “F” tables (code below).

The reason why I am doing this instead of one table because I want to assign names for different word categories to be able use them on collision event.

WORDS = { M = {"John", "Steve", "Kris", "Ben"}, F = {"Katie", "Susan", "Patricia", "Elizabeth"} } M.name = M F.name = F
local random = math.random WORDS = { M = {"John", "Steve", "Kris", "Ben"}, F = {"Katie", "Susan", "Patricia", "Elizabeth"} } print (WORDS.M[random(#WORDS.M)], WORDS.F[random(#WORDS.F)])

I’ve used “#” to get the length of the tables. If you want to access a certain element, you can use:

WORDS.M[1]

Does this work for you?

Thanks, but it prints two words and I need just one from both tables. And It’s looks like there is no way to do this like that.

What if I keep both tables separate and use function witch half a time will take words from one table and other half from another one. Any ideas how to implement it? 

Unless you’re trying to write Call Of Duty in Corona, there is always a way. 

[lua]

local random = math.random

local sexes = {“M”, “F”}

local words = {

M =  {“John”, “Steve”, “Kris”, “Ben”},

F = {“Katie”, “Susan”, “Patricia”, “Elizabeth”}

}

local sexID = sexes[random(#sexes)]  – get whether to use M or F table

local wordID = random(#words[sexID]) – get a random word ID to use from chosen table

print ("WORD IS: "…words[wordID])   – get the word from the words table based on ID

[/lua]

“attempt to concatenate field ‘?’ (a nil value)”? I am getting this error here.

What do you mean by that? It gets one from each table. Don’t you want that?

Also, I have shown you the way to access those table elements. You can always edit the code to your needs.

My mistake. I was missing the part that branches off to the ‘M’ or ‘F’ table in the words table.

[lua]

print ("WORD IS: "…words[sexID][wordID])   – get the word from the words table based on ID

[/lua]

Thank you Nick, it works perfectly now.

I may not have understood your post, but try this code to see one way to do it:

-- -- Try this code EXACTLY -- local names1 = {"John", "Steve", "Kris", "Ben"} local names2 = {"Katie", "Susan", "Patricia", "Elizabeth"} for i = 1, 100 do local first = names1[math.random(#names1)] local second = names2[math.random(#names2)] local merged = first .. " " .. second print( i, merged ) end

While the above code will print random selections from both tables, it won’t be an evenly random distribution over the short course.  i.e. If you only iterate 3 times you may get repeats.

IMHO, a Shuffle Bag is really the way to go here.

Thanks for help and sorry for my bad formulated question.

I want to display one random word at a time from “WORDS” table, but I can’t get access for words in those nested  “M” and “F” tables (code below).

The reason why I am doing this instead of one table because I want to assign names for different word categories to be able use them on collision event.

WORDS = { M = {"John", "Steve", "Kris", "Ben"}, F = {"Katie", "Susan", "Patricia", "Elizabeth"} } M.name = M F.name = F
local random = math.random WORDS = { M = {"John", "Steve", "Kris", "Ben"}, F = {"Katie", "Susan", "Patricia", "Elizabeth"} } print (WORDS.M[random(#WORDS.M)], WORDS.F[random(#WORDS.F)])

I’ve used “#” to get the length of the tables. If you want to access a certain element, you can use:

WORDS.M[1]

Does this work for you?

Thanks, but it prints two words and I need just one from both tables. And It’s looks like there is no way to do this like that.

What if I keep both tables separate and use function witch half a time will take words from one table and other half from another one. Any ideas how to implement it? 

Unless you’re trying to write Call Of Duty in Corona, there is always a way. 

[lua]

local random = math.random

local sexes = {“M”, “F”}

local words = {

M =  {“John”, “Steve”, “Kris”, “Ben”},

F = {“Katie”, “Susan”, “Patricia”, “Elizabeth”}

}

local sexID = sexes[random(#sexes)]  – get whether to use M or F table

local wordID = random(#words[sexID]) – get a random word ID to use from chosen table

print ("WORD IS: "…words[wordID])   – get the word from the words table based on ID

[/lua]

“attempt to concatenate field ‘?’ (a nil value)”? I am getting this error here.

What do you mean by that? It gets one from each table. Don’t you want that?

Also, I have shown you the way to access those table elements. You can always edit the code to your needs.

My mistake. I was missing the part that branches off to the ‘M’ or ‘F’ table in the words table.

[lua]

print ("WORD IS: "…words[sexID][wordID])   – get the word from the words table based on ID

[/lua]

Thank you Nick, it works perfectly now.