배열을 어떻게 하면 빨리, 랜덤으로 섞을까... 해서 여러 방법을 시도했지만 번번히 좋은 결과를 얻지 못하였는데,

좋은 소스를 발견하였다.


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
   
If a comment was clear to you can you accept the answer? –  Rytis Alekna Aug 20 '12 at 14:02
   
Excellent. Answer Accepted :) –  Papa De Beau Aug 20 '12 at 22:00


트랙백을 확인할 수 있습니다

URL을 배껴둬서 트랙백을 보낼 수 있습니다

다른 카테고리의 글 목록

Programming 카테고리의 포스트를 톺아봅니다