forked from: 1行でArrayをシャッフルする

by cpu_t forked from 1行でArrayをシャッフルする (diff: 8)
♥2 | Line 42 | Modified 2011-04-25 14:59:01 | MIT License
play

ActionScript3 source code

/**
 * Copyright cpu_t ( http://wonderfl.net/user/cpu_t )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/40K4
 */

// forked from nemu90kWw's 1行でArrayをシャッフルする
package {

    import flash.display.*;
    import flash.text.*;

    public class Shuffle extends Sprite
    {
        private var textfield:TextField;
        
        function Shuffle()
        {
            var i:int, array:Array, text:String = "";
            
            text += "Array.sort(function():int{return int(Math.random()*3)-1});\nで、配列をシャッフルできる。\n\n";
            
            text += "◆シャッフルのテスト\n";
            array = new Array();
            for(i = 1; i <= 16; i++) {array.push(i);}
            text += "array = ["+array+"]\n\n";
            
            text += shuffle(array)+"\n";
            text += shuffle(array)+"\n";
            text += shuffle(array)+"\n";
            text += shuffle(array)+"\n";
            text += shuffle(array)+"\n";
            
            text += "\n◆分布のテスト\n";
            array = new Array();
            for(i = 1; i <= 4; i++) {array.push(i);}
            text += "array = ["+array+"]\n\n";
            
            var result:Object = {};
            for(i = 0; i < 65536*4; i++) {
                var r:* = shuffle(array)
                if(!result[r])result[r] = 0;
                result[r]++;
            }
            for(var str:String in result) {text += "["+str+"] = "+result[str]+"\n";}
            
            textfield = new TextField();
            textfield.x = textfield.y = 20;
            textfield.width = textfield.height = 400;
            textfield.text = text;
            addChild(textfield);
        }
        
        public function shuffle(array:Array):Array
        {
            return array.sort(function():int{return int(Math.random()*3)-1});
        }
    }
}