Skip to content

Instantly share code, notes, and snippets.

@a10y
Last active February 8, 2025 19:57
Show Gist options
  • Save a10y/b49584ac96677b0bd6b81461793dcce3 to your computer and use it in GitHub Desktop.
Save a10y/b49584ac96677b0bd6b81461793dcce3 to your computer and use it in GitHub Desktop.
"""
A simple simulation of the Monty Hall problem
"""
import random
ROUNDS = 100_000
WINS = 0
for round in range(ROUNDS):
# place the car uniformly at random
car_door = random.randint(1, 3)
# contestant picks a door uniformly at random
pick = random.randint(1, 3)
# host opens a door not picked by the user that contains a goat
for i in [1,2,3]:
if i == car_door or i == pick:
continue
open_door = i
break
# The user switches to the other non-opened door
for i in [1,2,3]:
if i == pick or i == open_door:
continue
pick = i
break
if pick == car_door:
WINS += 1
print(f"played {ROUNDS} rounds won {WINS} ({100.0*WINS / ROUNDS}%)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment