# Calling & running a random function from an array

**URL:** <https://forum.kirupa.com/t/calling-running-a-random-function-from-an-array/304167>\
**Category:** flash\
**Created:** [February 2, 2010, 5:39pm UTC](https://forum.kirupa.com/t/calling-running-a-random-function-from-an-array/304167 "2010-02-02T17:39:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![doh](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@doh](https://forum.kirupa.com/u/doh)\
**Post date:** [February 2, 2010, 5:39pm UTC](https://forum.kirupa.com/t/calling-running-a-random-function-from-an-array/304167/1 "2010-02-02T17:39:29Z")

</div>

Hi,  
I’ve seen similar threads on here but nothing quite works for me, so sorry if this has been previously answered.

I’m creating buttons dynamically in functions, and I want to load them ALL in randomly as you enter the frame.

Let’s say I’ve made an array, and it looks like this:  
ActionScript Code:  
[LEFT][COLOR=#000000] **var** [/COLOR] ButtonsArray:[COLOR=#0000FF]Array[/COLOR] = [COLOR=#000000] **new** [/COLOR] [COLOR=#0000FF]Array[/COLOR] COLOR=#000000[/COLOR];

ButtonsArray[COLOR=#000000][[/COLOR][COLOR=#000080]0[/COLOR][COLOR=#000000]][/COLOR] = ZeroButton;  
ButtonsArray[COLOR=#000000][[/COLOR][COLOR=#000080]1[/COLOR][COLOR=#000000]][/COLOR] = FirstButton;  
ButtonsArray[COLOR=#000000][[/COLOR][COLOR=#000080]2[/COLOR][COLOR=#000000]][/COLOR] = SecondButton;  
ButtonsArray[COLOR=#000000][[/COLOR][COLOR=#000080]3[/COLOR][COLOR=#000000]][/COLOR] = ThirdButton;  
ButtonsArray[COLOR=#000000][[/COLOR][COLOR=#000080]4[/COLOR][COLOR=#000000]][/COLOR] = FourthButton;  
ButtonsArray[COLOR=#000000][[/COLOR][COLOR=#000080]5[/COLOR][COLOR=#000000]][/COLOR] = FifthButton;  
[/LEFT]

Where each of the array properties is a function, eg. ZeroButton(); which calls the function creating the button.

I can get the code running if I try to call one random function from it, for example:

```
ActionScript Code:
[LEFT][COLOR=#000000] **var** [/COLOR] randomSection = ButtonsArray[COLOR=#000000][[/COLOR][COLOR=#0000FF]Math[/COLOR].[COLOR=#0000FF]floor[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]([/COLOR][COLOR=#0000FF]Math[/COLOR].[COLOR=#0000FF]random[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR]*ButtonsArray.[COLOR=#0000FF]length[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]][/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];

```

[/LEFT]

But does anyone have any suggestions as to how I run all the functions from the array at once, just in a random order? OnEnterFrame in some way?

I tried using this code, but it didn’t work as I hoped:

```
ActionScript Code:
[LEFT]n = [COLOR=#000080]6[/COLOR];

```

[COLOR=#0000FF]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000] **var** [/COLOR] i:[COLOR=#0000FF]Number[/COLOR] = [COLOR=#000080]0[/COLOR]; i \< n; i++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]  
[COLOR=#000000] **var** [/COLOR] randomSelection = [COLOR=#0000FF]Math[/COLOR].[COLOR=#0000FF]floor[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]([/COLOR][COLOR=#0000FF]Math[/COLOR].[COLOR=#0000FF]random[/COLOR]COLOR=#000000[/COLOR] \* ButtonsArray.[COLOR=#0000FF]length[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000])[/COLOR];  
[COLOR=#0000FF]trace[/COLOR][COLOR=#000000]([/COLOR][COLOR=#FF0000]"Random selection is "[/COLOR] + randomSelection[COLOR=#000000])[/COLOR];  
[COLOR=#0000FF]trace[/COLOR][COLOR=#000000]([/COLOR][COLOR=#FF0000]"Corresponding Array value is "[/COLOR] + ButtonsArray[COLOR=#000000][[/COLOR]randomSelection[COLOR=#000000]][/COLOR][COLOR=#000000])[/COLOR];  
buttonsArray.[COLOR=#0000FF]splice[/COLOR][COLOR=#000000]([/COLOR]randomSelection, [COLOR=#000080]1[/COLOR][COLOR=#000000])[/COLOR]COLOR=#000000[/COLOR];  
[/LEFT]

The trace clearly tells me the array has been sorted, but it also says the “Corresponding Array value is [type Function]” - does that mean it didn’t get called to run?

I’m not sure I understand arrays enough here…

Can anyone help, or give pointers to any helpful documentation?

Thanks in advance 🙂
