forked from: Math.random()*10+1 で整数はでるのか?
forked from Math.random()*10+1 で整数はでるのか? (diff: 5)
Math.random() * 10 + 1; で整数が出るのか? 意味はない。 @author jc at bk-zen.com
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/8H6d
*/
// forked from bkzen's Math.random()*10+1 で整数はでるのか?
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(); をひたすらまわして\n0が出ることはありえるのかどうか。\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();
//var i: int = n;
if ( n == 0 )
{
txt.appendText("総回数 : " + (totalCount + a + 1) + " + " + uint.MAX_VALUE + " * " + overCount + "\n出た整数 : " + n);
removeEventListener(Event.ENTER_FRAME, loop);
return;
}
}
totalCount += loopCount;
if (totalCount < loopCount) overCount ++;
}
}
}