AS3 Array Shuffle
Adobe Flash/ActionScript 3.0 2011. 12. 28. 17:33AS3 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.
'Adobe Flash > ActionScript 3.0' 카테고리의 다른 글
Adobe Flash Platform * IME 클래스 사용 (0) | 2012.01.16 |
---|---|
A* sindney (0) | 2012.01.09 |
중심점 회전 (0) | 2011.12.21 |
[Flex,AIR,ActionScript 3.0]Matrix를 이용해 이동,스케일링,회전,거울효과 적용 (0) | 2011.12.20 |
BitmapData scroll (0) | 2011.12.09 |