Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- # Define the constants
- DOORS = ["DOOR A", "DOOR B", "DOOR C"]
- NUMBER_OF_RUNS = 1_000_000
- SWITCH = False # should savant switch her chosen door?
- OUTPUT = True # output first runs
- # Initialize the win counter
- win_score = 0
- for i in range(NUMBER_OF_RUNS):
- # Randomly assign the car and savant's initial choice
- car = random.choice(DOORS)
- savant = random.choice(DOORS)
- # Monty opens a door that is neither the car nor savant's choice
- monty_options = [door for door in DOORS if door != car and door != savant]
- monty_open = random.choice(monty_options)
- # Determine the final choice based on switching strategy
- if SWITCH:
- savant_final = next(door for door in DOORS if door != savant and door != monty_open)
- else:
- savant_final = savant
- # Check if the savant wins
- if savant_final == car:
- win_score += 1
- # Optional output for the first 20 runs
- if OUTPUT and i < 20:
- win_status = "WON" if savant_final == car else "LOST"
- switch_status = "switched doors" if SWITCH else "did not switch doors"
- print(f"car: {car}, savant: {savant}, monty: {monty_open}, savant {switch_status} and {win_status}")
- # Print the final results
- win_percentage = (win_score / NUMBER_OF_RUNS) * 100
- print(f"Runs: {NUMBER_OF_RUNS}, Wins: {win_score}, Win Percentage: {win_percentage:.2f}%")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement