Advertisement
danielvgomes

SAVANT

Nov 19th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | Source Code | 0 0
  1. import random
  2.  
  3. # Define the constants
  4. DOORS = ["DOOR A", "DOOR B", "DOOR C"]
  5. NUMBER_OF_RUNS = 1_000_000
  6. SWITCH = False # should savant switch her chosen door?
  7. OUTPUT = True # output first runs
  8.  
  9. # Initialize the win counter
  10. win_score = 0
  11.  
  12. for i in range(NUMBER_OF_RUNS):
  13.     # Randomly assign the car and savant's initial choice
  14.     car = random.choice(DOORS)
  15.     savant = random.choice(DOORS)
  16.  
  17.     # Monty opens a door that is neither the car nor savant's choice
  18.     monty_options = [door for door in DOORS if door != car and door != savant]
  19.     monty_open = random.choice(monty_options)
  20.  
  21.     # Determine the final choice based on switching strategy
  22.     if SWITCH:
  23.         savant_final = next(door for door in DOORS if door != savant and door != monty_open)
  24.     else:
  25.         savant_final = savant
  26.  
  27.     # Check if the savant wins
  28.     if savant_final == car:
  29.         win_score += 1
  30.  
  31.     # Optional output for the first 20 runs
  32.     if OUTPUT and i < 20:
  33.         win_status = "WON" if savant_final == car else "LOST"
  34.         switch_status = "switched doors" if SWITCH else "did not switch doors"
  35.         print(f"car: {car}, savant: {savant}, monty: {monty_open}, savant {switch_status} and {win_status}")
  36.  
  37. # Print the final results
  38. win_percentage = (win_score / NUMBER_OF_RUNS) * 100
  39. print(f"Runs: {NUMBER_OF_RUNS}, Wins: {win_score}, Win Percentage: {win_percentage:.2f}%")
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement