배열을 어떻게 하면 빨리, 랜덤으로 섞을까... 해서 여러 방법을 시도했지만 번번히 좋은 결과를 얻지 못하였는데,
좋은 소스를 발견하였다.
AS3.0 기준으로, an = arr.sort(randomize) 을 하면 an은 arr의 구성요소가 무작위로 섞인 배열이 된다.
밑의 소스는 배열에 넣지 않고 바로 trace 한 것.
var arr : Array = [0,1,2,3,4,5,6,7,8,9];
function randomize ( a : *, b : * ) : int {
return ( Math.random() > .5 ) ? 1 : -1;
}
trace( arr.sort( randomize ) );
출처 : http://stackoverflow.com/questions/11980657/as3-random-array-randomize-array-actionscript-3
아래와 같이 'Excellent'한 기분을 느낄 수 있다.
동작방식이 한 눈에 들어오지는 않지만, 일단 써놓은 뒤, 동작 방식을 분석하는 것도 나쁘지 않은 선택이다.
Agreed, more efficient and there's no need to round – BadFeelingAboutThis Aug 16 '12 at 16:31 | |||
This is pretty AWESOME. Can you break down the lines and explain what they are doing? Like what is the ( a : *, b : * ) and the sort etc.. – Papa De Beau Aug 16 '12 at 16:59 | |||
'Programming' 카테고리의 다른 글
YGN 이용, 멀티플레이 구현 (Yahoo Games Network)(구 Player.io) (0) | 2015.08.11 |
---|---|
[AS3] 소켓 통신 시 데이터가 밀린다면? (0) | 2015.01.23 |