# Need help with Arrays!

**URL:** <https://forum.kirupa.com/t/need-help-with-arrays/8543>\
**Category:** flash\
**Created:** [November 23, 2002, 8:22am UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543 "2002-11-23T08:22:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![byis](https://avatars.discourse-cdn.com/v4/letter/b/5e9695/32.png) [@byis](https://forum.kirupa.com/u/byis)\
**Post date:** [November 23, 2002, 8:22am UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/1 "2002-11-23T08:22:00Z")

</div>

Using the following array as an example:  
Array = [“Item1”,“Item2”,“Item3”,“etc”];

I am trying to figure out how to pull items from an array randomly. How do I setup the AS so when you click a button it will select a randon item from the array and display that item in dynamic text box? Sorry dont know AS that well but trying to learn.

Thanks for the help!

---

<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:** [November 23, 2002, 8:44am UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/2 "2002-11-23T08:44:47Z")

</div>

Hmm, untested, but try this as an example.

Create a new file, create a dynamic text box, give it the var name “setArray” (no quotes).

Add these actions on the frame…

```auto
myArray = ["Item1", "Item2", "Item3", "etc"];
textVar = Math.floor(Math.random()*myArray.length);
setArray = myArray[textVar];

```

---

<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:** [November 23, 2002, 8:47am UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/3 "2002-11-23T08:47:27Z")

</div>

Now to change it with a button try this…

On the Frame…

```auto
myArray = ["Item1", "Item2", "Item3", "etc"];
changeVar = function () {
	textVar = Math.floor(Math.random()*myArray.length);
	_root.setArray = myArray[textVar];
};

```

Where the text box has the var name setArray.

Now on the button add this…

```auto
on (release) {
	changeVar();
}

```

Again untested, so let me know how it goes.

---

<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:** [November 23, 2002, 10:05am UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/4 "2002-11-23T10:05:07Z")

</div>

Well, it depends if you want to pull the elements of the array one by one until the array is empty (a bit of array manipulation there), or if you want to take 1 randomly like Lost did (you should explain that code a bit, by the way :)).

pom :cowboy:

---

<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:** [November 23, 2002, 11:56am UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/5 "2002-11-23T11:56:04Z")

</div>

I just want to take 1 randomly. i tried the code lost posted but didnt seem to work. I will try playing with it some more tomorrow but not knowing AS that well its hard to troubleshoot te code…Thanks for the help guys!

---

<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:** [November 23, 2002, 5:40pm UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/6 "2002-11-23T17:40:27Z")

</div>

My code worked for me :-\

I posted a fully commented .fla file of what I did.

Ilyas: I wasn’t even sure if it worked, I just kind of winged it, so I was going to explain it later…lol.

If you don’t understand something byis, just ask 🙂

---

<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:** [November 24, 2002, 4:13am UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/7 "2002-11-24T04:13:55Z")

</div>

Thanks Lost! I was putting the ‘setArray’ as an instance name instead of a var and thats why it was not working. Havnet had a chance to study the code yet but im sure I might have some questions…Thanks agian!

---

<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:** [November 24, 2002, 4:14am UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/8 "2002-11-24T04:14:26Z")

</div>

No problem 🙂

As I said… feel free to ask 🙂

---

<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:** [November 24, 2002, 4:38am UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/9 "2002-11-24T04:38:25Z")

</div>

Someone has to write an article about arrays, there are so many questions about them :-\ Anyone feels like doing this? [SIZE=1]lost? :beam:[/SIZE]

---

<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:** [November 24, 2002, 4:45am UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/10 "2002-11-24T04:45:45Z")

</div>

Hrmmm, I guess that depends on what needs to be written about them.

My knowledge of them is just pretty basic :-\ ☹ :\*(

---

<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:** [November 24, 2002, 11:35am UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/11 "2002-11-24T11:35:23Z")

</div>

> \*Originally posted by ilyaslamasse \*  
> \*\*Someone has to write an article about arrays, there are so many questions about them :-\ Anyone feels like doing this? [SIZE=1]lost? :beam:[/SIZE] \*\*

Here is a good one, macromedia technote: 🙂

[http://www.kirupaforum.com/showthread.php?s=&postid=61335#post61335](http://www.kirupaforum.com/showthread.php?s=&postid=61335#post61335)

---

<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:** [November 24, 2002, 1:03pm UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/12 "2002-11-24T13:03:45Z")

</div>

That’s a brilliant tutorial, h88. I put it in the best of Kirupa. [SIZE=1]I just hope people will read it…[/SIZE] :-\

---

<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:** [November 25, 2002, 4:10am UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/13 "2002-11-25T04:10:46Z")

</div>

That is a nice article. My problem with arrays is not understanding the array itselfs but how to manipulate them to do what you want…That article goes pretty in depth into that however.

On a second note, is there a site that has a list of the different cmds in AS and a desription for each? I checked on [actionscript.org](http://actionscript.org) and didnt see anything…they do have some nice tuturiols though.

---

<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:** [November 25, 2002, 5:46am UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/14 "2002-11-25T05:46:53Z")

</div>

> \*Originally posted by byis \*  
> \*\*On a second note, is there a site that has a list of the different cmds in AS and a desription for each? I checked on [actionscript.org](http://actionscript.org) and didnt see anything…they do have some nice tuturiols though. \*\*

If you use MX you could take a look at the AS reference. (its the little book by the expert/normal mode button)

if you scroll down there is a “yellow” book called index, open that up, and it is an alphabetizied list. its a pretty dry read, but there are a ton there, good info too, like which versions support a certain command and whatnot.

---

<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:** [November 25, 2002, 5:48am UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/15 "2002-11-25T05:48:26Z")

</div>

That is what I used to learn arrays.

I learned which commands were used with Arrays and tested how they worked… trial and error really.

That is how I learn most everything I do in AS. Just try something and keep going with it until you think you have a firm grasp on it.

---

<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:** [November 25, 2002, 9:24am UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/16 "2002-11-25T09:24:22Z")

</div>

> Math.floor(Math.random()\*myArray.length);

can someone explain this statement. I kinda have a “grasp” on it, but  
i don’t want to go on, if I don’t have it right.

array.length is how many elements in the array. (lets use 5 for my example.)  
math.random() returns a random number greater than or equal to zero, and less than 1.  
and floor will take the number from this expression and change it to the number that is equal to or rounded down.  
so if math.random gave us .3345 and length was 5. that ==1.6725  
then floor turns that into 1  
and then that would return the 2nd element in the array.  
right?

ok, so if that is right, wouldn’t it seem that the last element in the array would  
be chosen the least amount of times???

---

<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:** [November 25, 2002, 9:33am UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/17 "2002-11-25T09:33:30Z")

</div>

random() is deprecated, not even used.

Math.floor rounds down as you said, that is the only way to get to 0, which is where an array starts counting from.

So Math.floor has the chance to round down to 0, and multiplying it by the length of the array makes sure that it has a chance of hitting that last one as well without going past it and getting a blank (null) value.

---

<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:** [November 25, 2002, 10:05am UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/18 "2002-11-25T10:05:23Z")

</div>

when i mentioned random I meant math.random.  
probably should have put that to save confusion.

---

<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:** [November 25, 2002, 10:08am UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/19 "2002-11-25T10:08:13Z")

</div>

Oh ok, wasn’t sure since random() and Math.random() are different.

---

<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:** [November 25, 2002, 10:23am UTC](https://forum.kirupa.com/t/need-help-with-arrays/8543/20 "2002-11-25T10:23:23Z")

</div>

thanks for your help so far. one last thing.  
getting back to what I said earlier wouldn’t it seem that logicallly if flash was pulling (math)random numbers and then getting the floor of those numbers, that randomly generating the last element would happen the least amount of times?

[Next page](https://forum.kirupa.com/t/need-help-with-arrays/8543.md?page=2)
