-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmonty_hall_paradox.py
61 lines (44 loc) · 1.49 KB
/
monty_hall_paradox.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
55
56
57
58
59
60
61
"""Monty hall paradox
Wiki:
https://en.wikipedia.org/wiki/Monty_Hall_problem
"""
import random
from typing import Tuple
CAR, GOAT = 'car', 'goat'
def estimate_probability(number: int, num_goat: int =2) -> Tuple[float, float]:
"""Monty hall paradox
Wiki:
https://en.wikipedia.org/wiki/Monty_Hall_problem
Parameters
----------
:param number: number of repetitions of the experiment, n >= 1
:type number: int
:param num_goat: number of doors with goats, num_goat >= 1
:type num_goat: int
Returns
-------
:return: Tuple[float, float]
The first element is the probability without changing the choice.
The second element is probability with choice change.
"""
DOORS = [CAR] + [GOAT] * num_goat # pylint: disable=C0103
p_with_change = 0
p_without_change = 0
for _ in range(number):
doors = DOORS[:]
random.shuffle(doors)
player_choice = random.choice(doors)
p_without_change += (player_choice == CAR)
doors.remove(player_choice)
while len(doors) > 1:
doors.remove(GOAT)
p_with_change += (doors[0] == CAR)
return p_without_change / number, p_with_change / number
def main():
"""Демонстрация работы"""
num_goat = 2
for i in range(10_000, 110_000, 10_000):
p_1, p_2 = estimate_probability(i, num_goat)
print(f'{i: >6}: P without change {p_1:.6f}, P with change {p_2:.6f}')
if __name__ == '__main__':
main()