Skip to content

Instantly share code, notes, and snippets.

@Amir-P
Created March 26, 2021 06:46
Show Gist options
  • Save Amir-P/ee71afe5f16eb2ff61c7172b2430a2ee to your computer and use it in GitHub Desktop.
Save Amir-P/ee71afe5f16eb2ff61c7172b2430a2ee to your computer and use it in GitHub Desktop.
Monty Hall problem simulation in Python
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