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.
April 17th, 2009 at 14:16
Thank you for the quote! :)
June 15th, 2009 at 14:58
Absolutly perfect, thanks a lot!
July 13th, 2009 at 08:36
Thanks! Will need this :)
September 9th, 2009 at 17:47
Wow. Brilliant. Best, cleanest, and fastest array shuffle I have found. Thank you for sharing this.
March 18th, 2010 at 08:51
Excellent!
March 22nd, 2010 at 02:00
nice thank u!
March 23rd, 2010 at 14:29
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];
}
March 29th, 2010 at 21:28
Ta buddee :)
May 14th, 2010 at 14:37
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
May 14th, 2010 at 14:53
Thanks for that info MrSteel, good to have that reference here
February 10th, 2011 at 14:00
thanks, comes in handy … who cares fo 2 milliseconds ;)
August 29th, 2011 at 05:33
perfect suffling
October 5th, 2011 at 17:06
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;
}
October 17th, 2011 at 10:55
actually nothing is truly random in desktop computing :)
February 27th, 2012 at 15:25
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.