Skip to content

Instantly share code, notes, and snippets.

@vfrico
Created April 15, 2019 17:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vfrico/d10f468eb175f8b5fb785d64c8efa2f7 to your computer and use it in GitHub Desktop.
Save vfrico/d10f468eb175f8b5fb785d64c8efa2f7 to your computer and use it in GitHub Desktop.
#!/bin/env python3
import random
def get_random_exclude(*n):
while True:
ran = random.randint(0,2)
if ran not in n:
return ran
def montyhall(first_behaviour, second_behaviour):
doors = [False, False, False]
doors[random.randint(0,2)] = True
car = doors.index(True)
# First user decission
user1 = first_behaviour()
# Host shows a goat
host1 = get_random_exclude(user1, car)
# User decides again (he must know his first decission and the door where the goat is)
user2 = second_behaviour(user1, host1)
return doors[user2]
def test(n, behaviour):
results = {True: 0, False: 0}
if behaviour is "change":
behaviour = lambda x,y: ({x, y} ^ {0,1,2}).pop()
elif behaviour is "keep":
behaviour = lambda x, y: x
for i in range(0, n):
result = montyhall(lambda: random.randint(0,2), behaviour)
results[result] += 1
print(results)
@vfrico
Copy link
Author

vfrico commented Apr 15, 2019

Run with:

import monty_hall
monty_hall.test(1000000, "change")

or

import monty_hall
monty_hall.test(1000000, "keep")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment