Timer random duration

I am ultimately making a timer that fires at random intervals. (between ~2 and ~7 seconds)
My code is successful, but requires a “steady” timer to call a “random timer”.
Any thoughts on making it more efficient/less sloppy?

Thanks,
Dane

[lua]local cratespinning = 0
local function crateanimationfunction() --makes the crates spin

if #pulleycratespawns > 0 then
math.randomseed( os.time() )
local randnum = math.random(#pulleycratespawns)
pulleycratespawns[randnum]:prepare(“crateanimation”)
pulleycratespawns[randnum]:play(“crateanimation”)
end
cratespinning = 0
end

local function cratespinner() --waits for a crate to stop spinning
if cratespinning == 0 then
cratespinning = 1
math.randomseed( os.time() )
print(math.random(1,3))
local cratespinningtimer = timer.performWithDelay(2500*math.random(1,3), crateanimationfunction, 1) --makes random intervals
end
end
math.randomseed( os.time() )
local cratetimer = timer.performWithDelay(1000, cratespinner, 0) --regularly calls random timer function[/lua]
[import]uid: 117490 topic_id: 22453 reply_id: 322453[/import]

Little example of random timer;

[lua]local function test ()
timerRan = math.random(2000, 7000)
print (“Delay:”…timerRan)
local function runTimer ()
timer.performWithDelay(timerRan, test, 1)
end
runTimer()
end
test()[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 22453 reply_id: 89555[/import]

Thanks Peach, I implemented it.
I’m trying to wrap my head around what the computer is doing though, for future understanding.

Q1
As the computer comes to this code for the first time, does it call test() while at the same time continuing on through the code?

Q2
Then let’s say I click a button to change scenes, does the computer just stop this random-timer loop?

Q3
What determines whether the computer continues on with the main code when calling a function, instead of stopping and waiting for the function to end?

Dane [import]uid: 117490 topic_id: 22453 reply_id: 89558[/import]

A1 -
We call test() the first time to start everything moving; this is because we have a timer within the test function that is being recreated each time test is called - and is itself calling test.
The first run through gets a random number and starts the first timer, when that fires it starts the timer again with the new random number, so on and so forth.

A2 -
If you’re changing scenes you’d give the timer a name when you created it, eg;

myTimer = timer.performWithDelay(timerRan, test, 1)

Then you could cancel in scene change by doing;

if myTimer then
timer.cancel(myTimer)

A3 -
I’m not totally sure I understand the question; the program basically reads down the code, one step at a time, continuing when previous step is complete.

Peach :slight_smile: [import]uid: 52491 topic_id: 22453 reply_id: 89590[/import]

Well you created an infinite loop right?
As I run the code by hand, I come to test(), runtimer(), test(), runtimer()…
But I know that the computer doesn’t wait for that “loop” to end before going on with the rest of my code (otherwise it would never happen).

Surely it is the same story with just a regular timer in the code. The computer starts a timer going but at the same time continues working through the rest of the code, right? [import]uid: 117490 topic_id: 22453 reply_id: 89644[/import]

Well yes, I mean it would go on forever if not stopped - but normally I take an “infinite loop” to refer to the “bad” kind that is created unintentionally and messes things up badly. (I’ve created a few of those in my time, too ;))

The code would move forward as soon as it started the timer, correct.

Peach :slight_smile: [import]uid: 52491 topic_id: 22453 reply_id: 89785[/import]

Thread is old but in case anyone is interested a snippet that is reusable

function Utils:randomTimer(fromTimer, toTimer, call)  
 local function repeatAction()  
 local randomTime = math.random(fromTimer, toTimer)  
 call()  
 timer.performWithDelay(randomTime, repeatAction, 1)  
 end  
 timer.performWithDelay(math.random(fromTimer, toTimer), repeatAction, 1)  
end  

You should be able to call this for eg:

Utils:randomTimer(2000, 4000, function() print('hello') end )  

Also should use something like transition and timer management library posted by Lerg:
http://developer.coronalabs.com/code/pausable-timers-and-transitions-speed-adjustment [import]uid: 135028 topic_id: 22453 reply_id: 138706[/import]

Thread is old but in case anyone is interested a snippet that is reusable

function Utils:randomTimer(fromTimer, toTimer, call)  
 local function repeatAction()  
 local randomTime = math.random(fromTimer, toTimer)  
 call()  
 timer.performWithDelay(randomTime, repeatAction, 1)  
 end  
 timer.performWithDelay(math.random(fromTimer, toTimer), repeatAction, 1)  
end  

You should be able to call this for eg:

Utils:randomTimer(2000, 4000, function() print('hello') end )  

Also should use something like transition and timer management library posted by Lerg:
http://developer.coronalabs.com/code/pausable-timers-and-transitions-speed-adjustment [import]uid: 135028 topic_id: 22453 reply_id: 138706[/import]