Last active
January 20, 2025 11:59
-
-
Save rasadeghnasab/54811a89998e75f4d97a87c2dfc809e6 to your computer and use it in GitHub Desktop.
montyHallSimulation
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
// Monty Hall Problem Simulation | |
function montyHallSimulation(iterations = 10000, numDoors = 3) { | |
let switchWinCount = 0; // Wins when switching choice | |
let stayWinCount = 0; // Wins when staying with initial choice | |
for ( | |
let currentIteration = 0; | |
currentIteration < iterations; | |
currentIteration++ | |
) { | |
// Randomly place the car | |
const carDoor = Math.floor(Math.random() * numDoors); | |
// Player's random choice | |
const playerChoice = Math.floor(Math.random() * numDoors); | |
// Record results based on switching or staying | |
if (playerChoice === carDoor) { | |
stayWinCount++; | |
} else { | |
switchWinCount++; | |
} | |
} | |
return { | |
"Number of Doors": numDoors, | |
"Number of Iterations": iterations, | |
"Switching Wins": switchWinCount, | |
"Staying Wins": stayWinCount, | |
"Probability of winning when switching": ( | |
(switchWinCount / iterations) * | |
100 | |
).toFixed(2), | |
"Probability of winning when staying": ( | |
(stayWinCount / iterations) * | |
100 | |
).toFixed(2), | |
}; | |
} | |
// /* | |
// Single execution | |
const totalDoors = 4; | |
const iterationSteps = 1; | |
const totalIterations = 100; | |
const simulationResult = montyHallSimulation(totalIterations, totalDoors); | |
console.log({ simulationResult }); | |
// */ | |
/* | |
// multiple execution for different total door numbers | |
const totalDoors = 30; | |
const iterationSteps = 1; | |
const totalIterations = 100; | |
for (let doors = 3; doors <= maxDoors; doors+=steps) { | |
// Run the simulation with customizable number of doors | |
const output = [] | |
let switchProbabilitySum = stayProbabilitySum = 0; | |
for (let doorsIteration = 0; doorsIteration < 5; doorsIteration++) { | |
const simulationResult = montyHallSimulation(totalIterations, doors); | |
switchProbabilitySum += parseFloat(simulationResult["Probability of winning when switching"]) | |
stayProbabilitySum += parseFloat(simulationResult["Probability of winning when staying"]) | |
output.push(simulationResult) | |
} | |
console.log({switchProbabilitySum: switchProbabilitySum / 5, stayProbabilitySum: stayProbabilitySum / 5, output,}) | |
} | |
// */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Monty Hall Problem: A Counterintuitive Probability Puzzle
Have you ever heard of the Monty Hall Problem? It's a fascinating probability puzzle that challenges our intuition about choice, strategy, and probabilities. Let me break it down for you:
You're on a game show with three doors. Behind one door is a car (the prize), and behind the other two are goats.
You pick one door.
The host, who knows what’s behind each door, opens another door, revealing a goat.
Now you’re given a choice: stick with your original door or switch to the remaining unopened door.
The big question is: Should you switch or stay?
The intuitive answer might be that it doesn’t matter—you think there’s a 50/50 chance of winning either way. But here's the twist: you’re twice as likely to win if you switch!
This happens because the probabilities shift once the host reveals a goat. The math might sound tricky, but simulations like the one below can make it crystal clear.
Here’s a JavaScript simulation I wrote that demonstrates the Monty Hall Problem. You can customize the number of doors, iterations, and see how the results change!