Math.random()*10+1 で整数はでるのか?

by bkzen
Math.random() * 10 + 1; で整数が出るのか?
意味はない。
@author jc at bk-zen.com
♥0 | Line 43 | Modified 2009-12-22 12:51:52 | MIT License
play

ActionScript3 source code

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

package  
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	
	/**
	 * Math.random() * 10 + 1; で整数が出るのか?
	 * 意味はない。
	 * @author jc at bk-zen.com
	 */
	public class TestAS extends Sprite
	{
		private var txt: TextField;
		private var totalCount: uint = 0;
		private var overCount: uint = 0;
		private const loopCount: uint = 1000000;
		
		public function TestAS() 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e: Event = null): void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			//
			addChild(txt = new TextField());
			txt.width = stage.stageWidth;
			txt.height = stage.stageHeight;
			txt.text = "Math.random() * 10 + 1; をひたすらまわして\n整数が出ることはありえるのかどうか。\n";
			addEventListener(Event.ENTER_FRAME, loop);
		}
		
		private function loop(e: Event ): void 
		{
			for (var a:int = 0; a < loopCount; a++) 
			{
				var n: Number = Math.random() * 10 + 1;
				var i: int = n;
				if (i == n) 
				{
					txt.appendText("総回数 : " + (totalCount + a + 1) + " + " + uint.MAX_VALUE + " * " + overCount + "\n出た整数 : " + n);
					removeEventListener(Event.ENTER_FRAME, loop);
					return;
				}
			}
			totalCount += loopCount;
			if (totalCount < loopCount) overCount ++;
		}
		
	}

}

Forked