Random for a bunch of functions

Hi, i’m trying to calla a math.random between a group of functions but i can’t.

What i basically have is:

[lua]local function f1()
for i = 1,2 do
x
end

local function f2()
for i = 3,4 do
y
end[/lua]

I tried grouping the functions like this:

[lua]local funcs {
func1 = f1
func2 = f2
}

–to get this working
local function randomm()
math.random(funcs[1],funcs[2]
end

randomm()[/lua]
But it simply doesn’t work. I’ve minimized the code to make it easier to check.
Basically i want to make some functions and then, in other function, get a random one if x condition is true.
I’d appreciate any help.
Thanks [import]uid: 108461 topic_id: 34903 reply_id: 334903[/import]

math.random takes two integers: a minimum value and a maximum value. You can pass a function to math.random but those functions would return an integer to provide those values. Your functions don’t return anything.

If you want to run a function randomly, perhaps something like this:

local function f1()  
for i = 1,2 do   
x  
end  
   
local function f2()  
for i = 3,4 do  
y  
end  
  
local funcs = {}  
funcs[1] = f1  
funcs[2] = f2  
   
--to get this working  
local function randomm()  
 return funcs[math.random(1,2)]  
end  
   
randomm()  

[import]uid: 199310 topic_id: 34903 reply_id: 138730[/import]

Thanks for the reply Rob.
I’ve adjusted it to my code and it worked fine.
I’m having problems with another function (the randomm() one) but i’ll check it out when i get home.

Thanks. [import]uid: 108461 topic_id: 34903 reply_id: 138999[/import]

Probably because there’s a typo in rob’s code - the last ) should be ]

:wink: [import]uid: 41884 topic_id: 34903 reply_id: 139003[/import]

Oops. Fixed. [import]uid: 199310 topic_id: 34903 reply_id: 139096[/import]

math.random takes two integers: a minimum value and a maximum value. You can pass a function to math.random but those functions would return an integer to provide those values. Your functions don’t return anything.

If you want to run a function randomly, perhaps something like this:

local function f1()  
for i = 1,2 do   
x  
end  
   
local function f2()  
for i = 3,4 do  
y  
end  
  
local funcs = {}  
funcs[1] = f1  
funcs[2] = f2  
   
--to get this working  
local function randomm()  
 return funcs[math.random(1,2)]  
end  
   
randomm()  

[import]uid: 199310 topic_id: 34903 reply_id: 138730[/import]

Thanks for the reply Rob.
I’ve adjusted it to my code and it worked fine.
I’m having problems with another function (the randomm() one) but i’ll check it out when i get home.

Thanks. [import]uid: 108461 topic_id: 34903 reply_id: 138999[/import]

Probably because there’s a typo in rob’s code - the last ) should be ]

:wink: [import]uid: 41884 topic_id: 34903 reply_id: 139003[/import]

Oops. Fixed. [import]uid: 199310 topic_id: 34903 reply_id: 139096[/import]

Hi I am a bit confused and trying to make sense of the code above. Here’s what I have so far and for simplicity sake, I have the following:

[lua]

btn = display.newImage(“btn.png”)

local function 1(event)

if event.phase == “began” then

move x and y etc

end

end

local function 2(event)

if event.phase == “began” then

move x and y etc

end

end

local function 3(event)

if event.phase == “began” then

move x and y etc

end

end

local functiontable = {}

functiontable[1] = function 1

functiontable[2] = function 2

functiontable[3] = function 3

local function Randomm(event)

return functiontable[math.random(1,3)]

end

–than i want to have the btn play it so i put in

btn:addEventListener(“touch”, Randomm)

[/lua]

Nothing happens when I press the btn, it doesn’t do any random thing, am I missing something here?

Hi I am a bit confused and trying to make sense of the code above. Here’s what I have so far and for simplicity sake, I have the following:

[lua]

btn = display.newImage(“btn.png”)

local function 1(event)

if event.phase == “began” then

move x and y etc

end

end

local function 2(event)

if event.phase == “began” then

move x and y etc

end

end

local function 3(event)

if event.phase == “began” then

move x and y etc

end

end

local functiontable = {}

functiontable[1] = function 1

functiontable[2] = function 2

functiontable[3] = function 3

local function Randomm(event)

return functiontable[math.random(1,3)]

end

–than i want to have the btn play it so i put in

btn:addEventListener(“touch”, Randomm)

[/lua]

Nothing happens when I press the btn, it doesn’t do any random thing, am I missing something here?