Guest User

Untitled

a guest
Mar 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # This is simulating Monty Hall Paradox
  3.  
  4. import random
  5.  
  6. def monty(switch):
  7. # random shuffle
  8. doors = [0, 0, 1] # one of the door contains the car
  9. random.shuffle(doors)
  10.  
  11. openDoor = None
  12.  
  13. # choose the first door (not open)
  14. # if the first door is 1, randomly open the other
  15. if doors[0] == 1:
  16. # open the door
  17. openDoor = random.randint(1, 2)
  18. else: # open the door that contains goat
  19. if doors[1] == 1:
  20. openDoor = 2
  21. else:
  22. openDoor = 1
  23.  
  24. # now open the last door
  25. if not switch:
  26. return doors[0]
  27. else:
  28. if openDoor == 2:
  29. return doors[1]
  30. else:
  31. return doors[2]
  32.  
  33. def main():
  34. total = 10000
  35. car = 0
  36. for i in range(total):
  37. car += monty(True)
  38.  
  39. print("Always switch the door. Total: {}, car: {}. P = {}".format(total, car, car / total))
  40.  
  41. car = 0
  42. for i in range(total):
  43. car += monty(False)
  44.  
  45. print("No switch the door. Total: {}, car: {}. P = {}".format(total, car, car / total))
  46.  
  47. main()
Add Comment
Please, Sign In to add comment