7 Replies Latest reply on Jul 2, 2013 9:52 AM by Ned Murphy

# Shuffling an array

I would like to shuffle this array:

private var  lettersL1:Array = [a,a,f,f,f,g,h,i,i,n,n,n,o,o,o,o,s,s,t,t,t];

for (var i: Number  =0; i < lettersL1.length; i++)

{

trace (lettersL1[i]);

}

I tried this:

lettersL1.push[i];

var shuffledArray:Array = new Array [];

while (lettersL1.length > 0)

{

var r:int = Math.floor(Math.random()*lettersL1.length);

shuffledArray.push(lettersL1[i]);

lettersL1[i].slice[r,1];

}

trace(shuffledArray[r]);

Where did I go wrong. Is there a better way?

Thanks

Charine

• ###### 1. Re: Shuffling an array

Use the shuffle function shown below (AS3 version):

function shuffle(a:Array) {
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--) {
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a[p];
a[p] = t;
}
}

// Usage:    shuffle(yourArray);

• ###### 2. Re: Shuffling an array

Hi Ned,

So how do I implement this up to my own code? The array I want to shuffle is called letterArray1 and I successfulle trace the unshuffled array with- trace (lettersL1[i]);

Thanks

Charine

• ###### 3. Re: Shuffling an array

Hi Charine,

Just copy the function which Ned has posted above as it is and call the same wherever you need it.

shuffle(lettersL1);

Regards,

Dharmendra.

• ###### 4. Re: Shuffling an array

Ok Great,

I need if for all 30 (Arrays) levels of my game. Do I put this in the constrctor function and call it later where I need it?

At the moment I have it in a second function, but when I said:

shuffle(lettersL1[index]);

I get this error message:

 1120: Access of undefined property index.

I'm pretty sure the (lettersL1[index]) is correct because I used the below in my constructors function with no problems.

lettersL1[index].x = 399; and lettersL1[index].y= 30;

In baby steps:

How do I trace this shuffled array?

Rgds,

Charine

• ###### 5. Re: Shuffling an array

The error is indicating that "index" is undefined.  So you need to find out why and fix that.

To trace an array just use trace(the_array); where the_array is whatever array you want to trace.

• ###### 6. Re: Shuffling an array

Great thanks Ned,

Everything works perfectly now.

I didn't realise that although I made a loop adding "index" that when I refer to the same array again I must keep to the original name. Somehow I thought that everytime you do something to an array, everytime the Array gets a new name so when you use it in the end it has all the functionality you added along the way.

What I thing I've leant:

Array name stays the same.

You can loop,

shuffle

when you use the array just render it in the order you want it e.g. after all the functions have been written.

Am I correct?

When I use your shuffle function, just one random letter from the array appears where I want it. Just as I want (but I thought I still had to write a place on screen type function). This is perfect. I just hope my code is correct.

Thank you so much for your help.

Regards,

Charine

• ###### 7. Re: Shuffling an array

You're welcome Charine