Created
March 26, 2021 06:46
-
-
Save Amir-P/ee71afe5f16eb2ff61c7172b2430a2ee to your computer and use it in GitHub Desktop.
Monty Hall problem simulation in Python
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
import random | |
iterations = 10000 | |
change_selection = True | |
winning_result = [] | |
for i in range(iterations): | |
doors = [0, 0, 0] | |
doors[random.randint(0, 2)] = 1 | |
possible_selection = [0, 1, 2] | |
selection = possible_selection[random.randint(0, len(possible_selection) - 1)] | |
possible_selection.remove(selection) | |
goat_doors = [door for door in possible_selection if doors[door] == 0] | |
revealed_door = goat_doors[random.randint(0, len(goat_doors) - 1)] | |
possible_selection.remove(revealed_door) | |
if change_selection: | |
selection = possible_selection[0] | |
winning_result.append(doors[selection] == 1) | |
print('win rate:', float(sum(winning_result)) / iterations) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment