# Random

**URL:** <https://forum.kirupa.com/t/random/19203>\
**Category:** Uncategorized\
**Created:** [April 25, 2003, 7:19am UTC](https://forum.kirupa.com/t/random/19203 "2003-04-25T07:19:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![niyaz](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@niyaz](https://forum.kirupa.com/u/niyaz)\
**Post date:** [April 25, 2003, 7:19am UTC](https://forum.kirupa.com/t/random/19203/1 "2003-04-25T07:19:30Z")

</div>

if i wanted to have a function that returned a random number in flash mx, would it generate a new random number each time its called, each time the frame is entered or only once ever?

in php in generates a new random number each time the function is called but that doesnt seem to be happening here.

thanks for any input

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 25, 2003, 7:36am UTC](https://forum.kirupa.com/t/random/19203/2 "2003-04-25T07:36:26Z")

</div>

Well, it all depends on how you’ve set it up exactly.

But if the random number is generated within a function, it will only be generated when the function is called.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 25, 2003, 7:52am UTC](https://forum.kirupa.com/t/random/19203/3 "2003-04-25T07:52:47Z")

</div>

function headshake() {  
A = math.floor(math.random() \* 1000)  
return A  
}

is approximately what i had. is there something wrong with that? it should generate a number everytime it gets that frame (because each time it calls the function again.)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 25, 2003, 8:11am UTC](https://forum.kirupa.com/t/random/19203/4 "2003-04-25T08:11:53Z")

</div>

a) is the function defined on the main timeline  
and  
b) when you call it do you call “\_root.headShake();”

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 25, 2003, 8:21am UTC](https://forum.kirupa.com/t/random/19203/5 "2003-04-25T08:21:37Z")

</div>

the function is defined in a movie clip in a movie clip, and i call it in the same frame that its defined in (more than once). should i just define it in teh main timeline and see what happens?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 25, 2003, 8:22am UTC](https://forum.kirupa.com/t/random/19203/6 "2003-04-25T08:22:43Z")

</div>

To make sure you get different number everytime, use time object to generate random number. I used to use it all the time when I code in java. time is good because it’s always changing.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 25, 2003, 8:38am UTC](https://forum.kirupa.com/t/random/19203/7 "2003-04-25T08:38:32Z")

</div>

Karzchix is 100% right on that. The time object is nice for that.

I would define the function in the \_root timeline and then call out to it by a call to \_root.headShake();, yes. I’m not sure if that is causing your problem or not… but it’s easier to figure out what’s going wrong if you don’t have a lot of path issues to deal with.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 25, 2003, 9:15am UTC](https://forum.kirupa.com/t/random/19203/8 "2003-04-25T09:15:10Z")

</div>

how would i use time to do that?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 25, 2003, 9:21am UTC](https://forum.kirupa.com/t/random/19203/9 "2003-04-25T09:21:45Z")

</div>

I think it goes like this

vA=Math.random()\*getTimer()

though I’ve never used it myself. Looks right though.
