# How do I call a function at random times?

**URL:** <https://forum.kirupa.com/t/how-do-i-call-a-function-at-random-times/265350>\
**Category:** flash\
**Created:** [July 7, 2008, 11:03pm UTC](https://forum.kirupa.com/t/how-do-i-call-a-function-at-random-times/265350 "2008-07-07T23:03:34Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![REEF](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@REEF](https://forum.kirupa.com/u/REEF)\
**Post date:** [July 7, 2008, 11:03pm UTC](https://forum.kirupa.com/t/how-do-i-call-a-function-at-random-times/265350/1 "2008-07-07T23:03:34Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![REEF](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@REEF](https://forum.kirupa.com/u/REEF)\
**Post date:** [July 7, 2008, 11:10pm UTC](https://forum.kirupa.com/t/how-do-i-call-a-function-at-random-times/265350/2 "2008-07-07T23:10:00Z")

</div>

Wow silly me, heres how its done:

```auto

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);

```

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [July 7, 2008, 11:14pm UTC](https://forum.kirupa.com/t/how-do-i-call-a-function-at-random-times/265350/3 "2008-07-07T23:14:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [July 7, 2008, 11:18pm UTC](https://forum.kirupa.com/t/how-do-i-call-a-function-at-random-times/265350/4 "2008-07-07T23:18:41Z")

</div>

```auto
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.

---

<div class="post-metadata">

**Author:** ![REEF](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@REEF](https://forum.kirupa.com/u/REEF)\
**Post date:** [July 7, 2008, 11:20pm UTC](https://forum.kirupa.com/t/how-do-i-call-a-function-at-random-times/265350/5 "2008-07-07T23:20:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [July 7, 2008, 11:22pm UTC](https://forum.kirupa.com/t/how-do-i-call-a-function-at-random-times/265350/6 "2008-07-07T23:22:40Z")

</div>

```auto
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);

```

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [July 7, 2008, 11:25pm UTC](https://forum.kirupa.com/t/how-do-i-call-a-function-at-random-times/265350/7 "2008-07-07T23:25:01Z")

</div>

I think you mean:

```auto

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.

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [July 7, 2008, 11:31pm UTC](https://forum.kirupa.com/t/how-do-i-call-a-function-at-random-times/265350/8 "2008-07-07T23:31:48Z")

</div>

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

```auto

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:

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

```

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [July 7, 2008, 11:39pm UTC](https://forum.kirupa.com/t/how-do-i-call-a-function-at-random-times/265350/9 "2008-07-07T23:39:34Z")

</div>

[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:

```auto

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.

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [July 7, 2008, 11:57pm UTC](https://forum.kirupa.com/t/how-do-i-call-a-function-at-random-times/265350/10 "2008-07-07T23:57:48Z")

</div>

> 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. 😛 And of course, I would have posted an example after you asked if they hadn’t beaten me to it. 🙂

---

<div class="post-metadata">

**Author:** ![REEF](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@REEF](https://forum.kirupa.com/u/REEF)\
**Post date:** [July 8, 2008, 12:03am UTC](https://forum.kirupa.com/t/how-do-i-call-a-function-at-random-times/265350/11 "2008-07-08T00:03:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [July 8, 2008, 12:07am UTC](https://forum.kirupa.com/t/how-do-i-call-a-function-at-random-times/265350/12 "2008-07-08T00:07:21Z")

</div>

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:

> **[How to find out what number in an Array a particular element is?](https://www.kirupa.com/forum/showthread.php?t=296801)**
>
> Got a web design/development question or just want to hang out with the most awesome people on the planet? Drop on in!

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [July 8, 2008, 12:18am UTC](https://forum.kirupa.com/t/how-do-i-call-a-function-at-random-times/265350/13 "2008-07-08T00:18:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [July 8, 2008, 12:28am UTC](https://forum.kirupa.com/t/how-do-i-call-a-function-at-random-times/265350/14 "2008-07-08T00:28:54Z")

</div>

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

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.
