flash on 2009-11-22

by takuya1021
さいころを1000回振って、出た目の割合をグラフで表示
♥0 | Line 31 | Modified 2009-11-22 16:11:18 | MIT License
play

ActionScript3 source code

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

//さいころを1000回振って、出た目の割合をグラフで表示
package {
    import flash.display.Sprite;
    import flash.text.TextField;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            // write as3 code here..
            var list:Array = [0,0,0,0,0,0];
            for(var i:int = 0;i<1000;i++){
                list[Math.floor(Math.random()*6)]++;
            }
           displayArray(list);
        }
        public function displayArray(list:Array) :void {
            var tf:TextField = new TextField;
            var sum:int;
            var str:String;
            tf.height =465; 
            tf.width =465; 
            addChild(tf);
            for(var i:int = 0;i<6;i++){
                sum += list[i];
            }
            for( i = 0;i<6;i++){
                //tf.appendText((i+1) + ":" + list[i] +"("+Math.round(list[i]/sum*10000)/100+"\%)\n");
                str = "";
                for(var j = 0;j<Math.round(list[i]/sum*100);j++){
                    str +="*";
                }
                tf.appendText((i+1) + ":" + str+"\n");
            }
        }
    }
}