Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # This is simulating Monty Hall Paradox
- import random
- def monty(switch):
- # random shuffle
- doors = [0, 0, 1] # one of the door contains the car
- random.shuffle(doors)
- openDoor = None
- # choose the first door (not open)
- # if the first door is 1, randomly open the other
- if doors[0] == 1:
- # open the door
- openDoor = random.randint(1, 2)
- else: # open the door that contains goat
- if doors[1] == 1:
- openDoor = 2
- else:
- openDoor = 1
- # now open the last door
- if not switch:
- return doors[0]
- else:
- if openDoor == 2:
- return doors[1]
- else:
- return doors[2]
- def main():
- total = 10000
- car = 0
- for i in range(total):
- car += monty(True)
- print("Always switch the door. Total: {}, car: {}. P = {}".format(total, car, car / total))
- car = 0
- for i in range(total):
- car += monty(False)
- print("No switch the door. Total: {}, car: {}. P = {}".format(total, car, car / total))
- main()
Add Comment
Please, Sign In to add comment