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

    Shuffling an array

    Charine -FB buggoop Level 1

      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:




                          var shuffledArray:Array = new Array [];

                          while (lettersL1.length > 0)


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







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




        • 1. Re: Shuffling an array
          Ned Murphy Adobe Community Professional & MVP

          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--) {
                  t = a[ivar];
                  a[ivar] = a[p];
                  a[p] = t;


          // Usage:    shuffle(yourArray);

          1 person found this helpful
          • 2. Re: Shuffling an array
            Charine -FB buggoop Level 1

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




            • 3. Re: Shuffling an array
              dharmk Adobe Employee

              Hi Charine,


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






              1 person found this helpful
              • 4. Re: Shuffling an array
                Charine -FB buggoop Level 1

                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:




                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?




                • 5. Re: Shuffling an array
                  Ned Murphy Adobe Community Professional & MVP

                  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
                    Charine -FB buggoop Level 1

                    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,


                    add timer


                    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.



                    • 7. Re: Shuffling an array
                      Ned Murphy Adobe Community Professional & MVP

                      You're welcome Charine