-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB23499-Assignment02-IC252-Q5.py
43 lines (36 loc) · 1.35 KB
/
B23499-Assignment02-IC252-Q5.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
''' i have taken reference from a youtube video
channel Numberphile-https://youtu.be/7u6kFlWZOWg?feature=shared
and also taken help from some website to write the code '''
import random
import matplotlib.pyplot as plt
def monty_hall(switch_door, num_trials=10000):
wins = 0
result = []
for i in range(num_trials):
# Place the prize behind one of the doors
doors = [0, 0, 0]
prize = random.randint(0, 2)
doors[prize] = 1
choice = random.randint(0, 2) # Player's choice
#reveals one of the other doors, which does not have the prize
doors_to_reveal = [i for i in range(3) if i != choice and doors[i] == 0]
door_revealed = random.choice(doors_to_reveal)
# Player switches the choice if enabled
if switch_door:
choice = next(i for i in range(3) if i != choice and i != door_revealed)
# Check if the player wins
if doors[choice] == 1:
wins += 1
result.append(wins/(i+1))
return result
# return wins / num_trials
prob = monty_hall(True)
prob_not = monty_hall(False)
plt.plot(prob, label='Switching Doors')
plt.plot(prob_not, label='Not Switching Doors')
plt.title('Probability Distribution of Winning the Monty Hall Problem')
plt.xlabel('Number of Trials')
plt.ylabel('Probability of Winning')
plt.legend()
plt.grid(True)
plt.show()