math.random
math.random

Reputation: 99

Love2d: random element from table

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.

Upvotes: 1

Views: 958

Answers (1)

lhf
lhf

Reputation: 72422

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) 

Upvotes: 3

Related Questions