AS3 Array Shuffle

There are a lot of ways to shuffle an array in AS3, ranging from swapping repeatedly the elements to using the sort function with a random sorter, but the most convenient way I’ve found was to simply splice a random element from the former array to the new shuffled array while the former array has elements.

DaveOnCode has a nice implementation of this method, pasted here:

var arr2:Array = [];

while (arr.length > 0) {
    arr2.push(arr.splice(Math.round(Math.random() * (arr.length - 1)), 1)[0]);
}

Edit: Although small and convenient, this method is not the fastest way of shuffling an array, check out the comments for more info.

15 Responses to “AS3 Array Shuffle”

  1. DaveOnCode Says:

    Thank you for the quote! :)

  2. turtlebite Says:

    Absolutly perfect, thanks a lot!

  3. Funky Clouds Says:

    Thanks! Will need this :)

  4. Jo Albright Says:

    Wow. Brilliant. Best, cleanest, and fastest array shuffle I have found. Thank you for sharing this.

  5. Özgür Says:

    Excellent!

  6. xxxxGame Says:

    nice thank u!

  7. philtre Says:

    It would probably be a tad faster like this:

    var len:int = arr.length;
    var arr2:Array = new Array(len);
    for(var i:int = 0; i<len; i++)
    {
    arr2[i] = arr.splice(int(Math.random() * (len – i)), 1)[0];
    }

  8. chichilatte Says:

    Ta buddee :)

  9. MrSteel Says:

    hi

    nice way but it’s not the fastest
    take a look http://mrsteel.wordpress.com/2007/06/15/randomize-array-shuffle-an-array-in-flash/

    shuffle by slice is one of slowest methods actually

  10. Flassari Says:

    Thanks for that info MrSteel, good to have that reference here

  11. andreas Says:

    thanks, comes in handy … who cares fo 2 milliseconds ;)

  12. sid Says:

    perfect suffling

  13. andy Says:

    splicing is slow, but it also provides a truly random array… the quickest published way is biased toward keeping keeping earlier elements in the initial array earlier in the mixed array, here are my two utility randomizing methods:

    public static function randomizeArray(array:Array):Array
    {
    //l=length arr=temp parameter array mixed=randomized array i=index
    var l:int = array.length,arr = array.slice(),mixed:Array = new Array(l),i:int;
    for (i = 0; i<l; i++)
    {
    mixed[i] = arr.splice(int(Math.random() * (l – i)), 1)[0];
    }
    return mixed;
    }

    public static function randomizeArrayQuick(array:Array):Array
    {
    //This method is significantly faster, but not truly random
    //l=length mixed=randomized array r=random number i=index e=array element
    var l:Number = array.length,mixed:Array = array.slice(),r:Number,i:Number,e:Object;
    for (i = 0; i<l; i++)
    {
    e = mixed[i];
    r = Math.floor(Math.random() * l);
    mixed[i] = mixed[r];
    mixed[r] = e;
    }
    return mixed;
    }

  14. aiux Says:

    actually nothing is truly random in desktop computing :)

  15. Thales Says:

    Trying an Array: a[1,2,3,4,5,6,7,8,9,10],
    This function give us 5% chance of the first position stay with 1 and 20% of the last stay with 10.

    function shuffle3(array_arr:Array):Array{
    for(var i:Number = 0; i < array_arr.length; i++){
    var randomNum_num = Math.floor(Math.random() * array_arr.length);
    var arrayIndex = array_arr[i];
    array_arr[i] = array_arr[randomNum_num];
    array_arr[randomNum_num] = arrayIndex;
    }
    return array_arr;
    }

    this one have 10% for each position of that array.