-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonty.py
54 lines (45 loc) · 1.37 KB
/
monty.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#a solution to the monty hall paradox. Shows that you should always switch
#runs each 10,0000 times to get a fairly stable 33/66 split
#increase xrange for an even better approximation
from __future__ import division
import random
#returns percentage of wins without switching
def monty_stay():
win = 0
tries = 0
for x in xrange(1,10000):
doors = ["goat", "goat", "goat"]
prize_door = random.randint(0,2)
doors[prize_door] = "prize"
if doors[0] == "prize":
win += 1
tries += 1
return "same door win rate: " + str(((win/tries) * 100))
#returns percentage of wins always switching
def monty_switch():
win = 0
tries = 0
for x in xrange(1,10000):
doors = ["goat", "goat", "goat"]
prize_door = random.randint(0,2)
doors[prize_door] = "prize"
user_door = random.randint(0,2)
if doors[0] == "prize" and user_door == 0:
user_door = 1
elif doors[1] == "prize" and user_door == 1:
user_door = 0
elif doors[2] == "prize" and user_door == 2:
user_door = 0
elif doors[0] == "goat" and user_door == 0:
user_door = prize_door
elif doors[1] == "goat" and user_door == 1:
user_door = prize_door
elif doors[2] == "goat" and user_door == 2:
user_door = prize_door
if doors[user_door] == "prize":
win += 1
tries += 1
return "switch door win rate: " + str(((win/tries) * 100))
if __name__ == '__main__':
print monty_stay()
print monty_switch()