Skip to content

Instantly share code, notes, and snippets.

@VincentLoy
Created June 25, 2015 19:16
Show Gist options
  • Save VincentLoy/88c60f610e02213f8da7 to your computer and use it in GitHub Desktop.
Save VincentLoy/88c60f610e02213f8da7 to your computer and use it in GitHub Desktop.
Test with the Monty Hall problem
# vim: fileencoding=utf-8 tw=100 expandtab ts=4 sw=4 :
#
# monty-hall-problem
# copyright (c) 2015 Vincent Loy
# Vincent Loy <vincent.loy1@gmail.com>
import random
class MontyHall:
def __init__(self):
self.doors = sorted([True, False, False], key=lambda k: random.random())
self.selected = None
def reset_doors(self):
self.doors = sorted([True, False, False], key=lambda k: random.random())
def take_a_door(self):
self.selected = random.randint(0, 2)
return self.doors[self.selected]
def get_percentage(t):
win = 0
for r in t:
if r is True:
win += 1
return 100 - (100 * win) / len(t)
results = []
n = 10000
for x in range(0, n):
tmp = MontyHall()
results.append(tmp.take_a_door())
change_to_win_percentage = str(get_percentage(results))
print('in ' + change_to_win_percentage + '% of cases it was more appropriate to change your choice')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment