How do I call a function at random times?

function doit () {
trace(“hi”);
}

I need to call this at random times from 1 to 10 seconds. Figured out how to make a random interval, but cant get past this:

var minTime:Number = 1000;
var maxTimeNumber = 10000;
var time:Number = Math.round(Math.random() * (maxTime - minTime));

(basically trying to make a little boy have tear drops come out of his eye randomly)

Wow silly me, heres how its done:


minTime = 1000; // 1 second
maxTime = 5000; // 5 seconds
var time:Number = Math.round(Math.random() * (maxTime - minTime));
function doFunc():Void {
	clearInterval(myint);
	time = Math.round(Math.random() * (maxTime - minTime));
	myint = setInterval(doFunc, time);
	trace(time);
	// EXECUTE YOUR CODE HERE

}
myint = setInterval(doFunc, time);

It would make more sense to use setTimeout, I think, since you wouldn’t need to store the interval ID or clear the interval.

var minTime:int = 1000;
var maxTime:int = 10000;
var t:Timer = new Timer(Math.round(Math.random() * (maxTime - minTime)));
t.addEventListener(TimerEvent.TIMER, callRandom);
t.start();
function callRandom(evt:TimerEvent):void {
	evt.currentTarget.delay = Math.round(Math.random() * (maxTime - minTime));
	doit();
}
function doit():void {
	trace("done");
}

There’s another way, although it looks like you’re using AS2. This is why I hate the CS3 and 8 forums.

Kril: Got an example? Not sure how its any different.

TC: I can barely use 1.0, so yeah Im using 2.0. Cant use any 3.0 at all.

minTime = 1000;
maxTime = 5000;
var time:Number = Math.round(Math.random() * (maxTime - minTime));
function doFunc():Void {
	time = Math.round(Math.random() * (maxTime - minTime));
	setTimeout(doFunc, time);
	trace(time);
}
setTimeout(doFunc, time);

I think you mean:


var time:Number = int( Math.max(minTime, Math.random() * maxTime) );

The way you’re doing it will produce numbers below your minTime, and not quite as high as your maxTime.

[QUOTE=Anogar;2354981]I think you mean:


var time:Number = int( Math.max(minTime, Math.random() * maxTime) );

The way you’re doing it will produce numbers below your minTime, and not quite as high as your maxTime.[/QUOTE]That gives a much greater chance that the time will equal minTime which isn’t random.

Should be:

var time:Number = Math.floor(minTime + Math.random() * (maxTime - minTime));

[QUOTE=TheCanadian;2354984]That gives a much greater chance that the time will equal minTime which isn’t random.[/QUOTE]

Good point. I think this is actually right:


var time:Number = Math.floor(Math.random() * (maxTime - minTime + 1)) + minTime;

I think that’s fairly decent. The +1 accounts for the Math.floor - otherwise, given a range of 0 - 1, you’d only get 0, and never 1.

Kril: Got an example? Not sure how its any different.

Well, the reason that I didn’t post an example originally is because there are a bunch of blog posts and similar pages where someone discovers setTimeout and thinks that it’s nifty, so they make an unreasonably big deal about it. Those sites do a good job of providing simple examples, so I figured that you could just search Google and find out how to use it yourself, since it’s not really conceptually different at all.

Not that it matters now that TheCanadian and Anogar have posted some examples. :stuck_out_tongue: And of course, I would have posted an example after you asked if they hadn’t beaten me to it. :slight_smile:

Yeah they pretty much covered everything I needed, and more lol.

A setTimeout should, ideally, be cleared with clearTimeout() so you don’t inadvertently have a host of intervals running at the same time.

And yes, that +1 is significant to ensure an even distrubution of random numbers:

(Although really, when dealing with a range of 9,000, the +1 isn’t super noticeable - but it makes it more correct.)

Isn’t it useless to call clearTimeout if you want the function to be called at all?

I can see it being useful if someone wanted to stop the cycle, though.