perform a function randomly from a set/group of functions?

I have a beat em up game and i want my enemy to spawn random items when it’s killed.

for example i have spawnCoin as a function and spawnHealthitem spawnArmouritem etc. and i want each enemy to drop say 2 coins and a health one time or 4 coins and a armour another or just some coins one time.

I know there is a probably a real simple way to do this (probably using math.random somehow?) but can’t get my head around it so any help would be much appreciated!

Regards,

Finn M.

function spawnA()   print("A") end function spawnB()   print("B") end function spawnRandomStuff()   local spawnFunctions = {spawnA, spawnB}   for \_ = 1, 5 do     spawnFunctions[math.random(#spawnFunctions)]()   end end spawnRandomStuff()  

that’s great thanks a lot for the quick reply!

some other options

local spawn ={} spawn[1]=function() --spawn1 end spawn[2]=function() --spawn2 end local function spawnObject() for a =1,5 do spawn[math.random(1,2)]() end spawnObject()

or

local objects ={"obj1.png","obj2.png"} local function spawn() for a=1,5 do obj= display.newImage( objects[math.random(1,#objects)], ............) end end spawn()
function spawnA()   print("A") end function spawnB()   print("B") end function spawnRandomStuff()   local spawnFunctions = {spawnA, spawnB}   for \_ = 1, 5 do     spawnFunctions[math.random(#spawnFunctions)]()   end end spawnRandomStuff()  

that’s great thanks a lot for the quick reply!

some other options

local spawn ={} spawn[1]=function() --spawn1 end spawn[2]=function() --spawn2 end local function spawnObject() for a =1,5 do spawn[math.random(1,2)]() end spawnObject()

or

local objects ={"obj1.png","obj2.png"} local function spawn() for a=1,5 do obj= display.newImage( objects[math.random(1,#objects)], ............) end end spawn()