SOLVED - Randomly print a statement.

Hi,
I’m trying to have 3 print statements and when a input is given it randomly chooses one of them and prints it.

Printing to the console will be enough.

My main issue is working with tables.{} (incase i want to increase the number later on).
I imagine it’d be something like:

local printtable = {} -- Creation of table for print statements.  

Then i’d need to write the print statements into the table ad associate them with a number.
And then use the random function to call a number.

(math.random (3))  

And print that print statement out that correspondes.

I realise i’ve left alot of holes in this, but have i got the right idea on how to accomplish this?

Thanks,
Matt
[import]uid: 91798 topic_id: 16334 reply_id: 316334[/import]

  
local printtable = {}  
  
... some stuff to populate printtable  
  
print(printtable[math.random(#printtable)])  
  

#printtable returns the number of items in printtable.
math.random(a number) returns a random number between 1 and “a number”
printtable[somenumber] is a unique item in the table

[import]uid: 19626 topic_id: 16334 reply_id: 60858[/import]

Thanks that’s exactly it,
Thanks Robmiracle.

For anyone else who want to know, to populate your table i used.

printtable[1] = "Print statement 1"  
printtable[2] = "print statement 2"  
printtable[3] = "Print statement 3"  

Other ways are clearly written about here.
http://blog.anscamobile.com/2011/06/understanding-lua-tables-in-corona-sdk/

Thanks again! [import]uid: 91798 topic_id: 16334 reply_id: 60873[/import]

the easiest way to populate tables in lua is

local printtable = {
“Print Statement 1”,
“Print Statement 2”,
“Print Statement 3”
}

you can add more as you want without having to renumber the elements manually

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16334 reply_id: 60874[/import]

I chose the way i did it because it looked more uniform to me, it is just preferance afterall.

But you raise a valid point.
Thanks! ^^ [import]uid: 91798 topic_id: 16334 reply_id: 60882[/import]