Skip to content

Instantly share code, notes, and snippets.

@relaxedtomato
Created February 27, 2015 00:30
Show Gist options
  • Save relaxedtomato/66bd53db9034a85a52a7 to your computer and use it in GitHub Desktop.
Save relaxedtomato/66bd53db9034a85a52a7 to your computer and use it in GitHub Desktop.
Solving the Monty Hall Problem 1000 Times.
var doors = [];
var temp;
//NO SWITCH
var firstGuess;
var doorOpen;
var noSwtichcorrect=0;
var noSwitchwrong=0;
function placeCar(){
for(;;){
if((doors.length === 3)&&(doors.indexOf('car')===-1)){
doors = [];
continue;
}
else if((doors.length === 3)&&(doors.indexOf('car')!==-1)){
break;
}
else{
temp = Math.random(); //number between 0-1
if((temp >= 0.5)&&(doors.indexOf('car')===-1)){doors.push('car');}
else{doors.push('goat');}
}
}
}
//NO SWITCH
for(var i=0;i<1000;i++){
placeCar();
firstGuess = Math.floor(Math.random(1)*(4-1)+1);
if(doors.indexOf('car') === (firstGuess-1)){noSwtichcorrect++;}
else{noSwitchwrong++;}
}
console.log("No Switch Correct: "+noSwtichcorrect/10+"%, and No Switch Wrong: "+noSwitchwrong/10+"%");
var Swtichcorrect=0;
var Switchwrong=0;
//SWITCH
for(var j=0;j<1000;j++){
placeCar();
firstGuess = Math.floor(Math.random(1)*(4-1)+1);//1,2,3
for(;;){
doorOpen = Math.floor(Math.random(1)*(4-1)+1);//1,2,3
if(doorOpen === firstGuess){continue;}
else{break;}
}
if((firstGuess === 1)&&(doorOpen===2)){firstGuess=3;}
if((firstGuess === 1)&&(doorOpen===3)){firstGuess=2;}
if((firstGuess === 2)&&(doorOpen===1)){firstGuess=3;}
if((firstGuess === 2)&&(doorOpen===3)){firstGuess=1;}
if((firstGuess === 3)&&(doorOpen===1)){firstGuess=2;}
if((firstGuess === 3)&&(doorOpen===2)){firstGuess=1;}
if(doors.indexOf('car') === (firstGuess-1)){Swtichcorrect++;}
else{Switchwrong++;}
}
console.log("Switch Correct: "+Swtichcorrect/10+"%, and Switch Wrong: "+Switchwrong/10+"%");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment