package { import flash.display.MovieClip; import flash.events.TimerEvent; import flash.text.TextField; import flash.utils.Timer; /** * ... * @author Jaaq */ public class Trace extends MovieClip { private var doors:Array = [0, 1, 2]; private var numberOfGames:int = 100000; // changes the number of simulated games private var results = 0; public function Trace() { for (var i:int = 0; i < numberOfGames; i++) { // startGame(true) switches your choice // startGame(false) doesnt switch your choice //if (startShortGame(false)) results ++; if (startLongGame(false)) results ++; } trace(results / numberOfGames * 100 + " % "); } private function startShortGame(doSwitch:Boolean):Boolean { var doYouGetACar:Boolean; var winningDoor = randomRange(0, 2); var doorChosen:int = randomRange(0, 2); if(winningDoor != doorChosen){ // this happens 66% of the time if(doSwitch) doYouGetACar = true; // switching Wins if(!doSwitch) doYouGetACar = false // switching Loses } if(winningDoor == doorChosen){ // this happens 33% of the time if(doSwitch) doYouGetACar = false; // switching Loses if(!doSwitch) doYouGetACar = true // switching Winss } return doYouGetACar; } private function startLongGame(doSwitch:Boolean):Boolean { doors = [0, 1, 2]; var winningDoor = randomRange(0, 2); var doorChosen:int = randomRange(0, 2); if(doorChosen == winningDoor && doSwitch) return false; var doorGiven:int = giveDoor(doorChosen, winningDoor); if (doSwitch) { doors.splice(doors.indexOf(doorChosen), 1); doors.splice(doors.indexOf(doorGiven), 1); doorChosen = doors[0]; } if (winningDoor == doorChosen) return true; return false; } private function giveDoor(chosen:int,winner:int):int { var temp:Array = [0, 1, 2]; temp.splice(temp.indexOf(chosen), 1); temp.splice(temp.indexOf(winner), 1); return temp[0]; } function randomRange(minNum:Number, maxNum:Number):Number { return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum); } } }