AS3 Array Shuffle

Adobe Flash/ActionScript 3.0 2011. 12. 28. 17:33
반응형
http://flassari.is/2009/04/as3-array-shuffle/

AS3 Array Shuffle

There are alot 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.

반응형
: