Created
January 9, 2015 19:55
-
-
Save crullian/e2df04f30d2411afd33e to your computer and use it in GitHub Desktop.
The Monty Hall Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function monty() { | |
var runProblem = 1000; | |
var switchWins = 0; // = change answer | |
var switchLosses = 0; | |
var holdWins = 0; // = keep user_choice | |
var holdLosses = 0; | |
for (var i = 1; i <= runProblem; i++) { | |
// random user choice (what door they picked) | |
var user_choice = Math.floor(Math.random() * 3); | |
if (user_choice === 0) { | |
switchLosses++; | |
holdWins++; | |
} else if (user_choice === 1) { | |
switchWins++; | |
holdLosses++; | |
} else if (user_choice === 2) { | |
switchWins++; | |
holdLosses++; | |
} | |
} | |
// log a string with winning and losing percentages | |
console.log("Switching wins " + | |
Math.floor(switchWins * 100 / (switchWins + switchLosses)) + | |
"% of the time."); | |
// Wins pct with switch | |
console.log("Not switching wins " + | |
Math.floor(holdWins * 100 / (holdWins + holdLosses)) + | |
"% of the time."); | |
// Wins pct with hold | |
} | |
monty(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment