Math.random テスト

by tkinjo
Math.random テスト

@author tkinjo
♥0 | Line 39 | Modified 2010-01-01 09:18:30 | MIT License
play

ActionScript3 source code

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

package  
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    
    [SWF(width="465", height="465", backgroundColor="0xffffff", frameRate="60")] 
    /**
     * Math.random テスト
     * 
     * @author tkinjo
     */
    public class Main extends Sprite
    {
        private const MAX_NUM:uint = 10;
        
        private const LOOP:uint = 10000;
        
        private var outputTextField:TextField;
        
        public function Main() 
        {
            outputTextField = new TextField();
            outputTextField.autoSize = TextFieldAutoSize.LEFT;
            addChild( outputTextField );
            
            calc();
            
            stage.addEventListener(MouseEvent.CLICK, mouseClickHandler);
        }
        
        private function mouseClickHandler( event:MouseEvent ):void {
            
            calc();
        }
        
        private function calc():void {
            
            outputTextField.text = "";
            
            var randoms:Vector.<uint> = new Vector.<uint>( MAX_NUM );
            
            for ( var i:uint = 0; i < LOOP; i++ )
                randoms[ (uint)( Math.random() * MAX_NUM ) ]++;
            
            
            var counter:uint;
            
            for ( i = 0; i < randoms.length; i++ ) {
                
                outputTextField.appendText( i + " : " + randoms[ i ] + "\n" );
                counter += randoms[ i ];
            }
            
            outputTextField.appendText( "c : " + counter + "\n" );
        }
    }
}