1

I'm trying to get a item randomly from a table. I have searched online but all the code I have found didn't work. My table looks like this:

section = {a, b}
love.graphics.newImage("/Images/a.png")
love.graphics.newImage("/Images/b.png")     
love.graphics.draw(section[imath.random(#section)], x, y) 

I need a random item from this table.

2

1 Answer 1

3

Try this:

item = section[math.random(#section)]

In your example:

section = {
   love.graphics.newImage("/Images/a.png"),
   love.graphics.newImage("/Images/b.png"),
}    
love.graphics.draw(section[math.random(#section)], x, y) 
2
  • 1
    @math.random, if you want letters, use section = {"a", "b", "c", "f", "z"}.
    – lhf
    Commented Mar 2, 2015 at 18:13
  • i need this to load a random .png. This is the code: love.graphics.newImage("/Images/a.png") .... love.graphics.newImage("/Images/b.png") love.graphics.draw(section[imath.random(#section)], x, y) Commented Mar 2, 2015 at 18:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.