-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbertrand_simulation.py
More file actions
executable file
·76 lines (62 loc) · 2.53 KB
/
bertrand_simulation.py
File metadata and controls
executable file
·76 lines (62 loc) · 2.53 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/python3
import random
import sys
"""
Short script by Michael Zeevi to simulate the result of the Bertrand's box paradox:
There are three boxes:
1. a box containing two gold coins,
2. a box containing two silver coins,
3. a box containing one gold coin and a silver coin.
The 'paradox' is in the probability, after choosing a box at random and withdrawing
one coin at random, if that happens to be a gold coin, of the next coin also being
a gold coin.
Description from https://en.wikipedia.org/wiki/Bertrand's_box_paradox
17/1/2017
"""
def simulation(cases=10000):
counts = {
'cases': cases,
'first': {
'gold': 0,
'silver': 0
}, 'second': {
'gold': 0,
'silver': 0
}
}
print('\nRunning simulations...')
for _ in range(cases):
boxes = [
['gold', 'gold'],
['gold', 'silver'],
['silver', 'silver']
]
box = random.choice(boxes)
first = random.choice(box)
counts['first'][first] += 1
if first == 'gold':
box.remove('gold')
second = random.choice(box)
counts['second'][second] += 1
return counts
def main():
print('\n\tWelcome to Bertrand\'s box simulation by Michael Zeevi.')
print('\tFor more information see source code at github (https://github.com/maze88/bertrand_simulation).') # insert link!
print('\tAmount of test cases can be adjusted by adding optional argument after bertrand_simulation.py.')
print('\tDefault number of test cases is 10000. Recommended value: 10^4 < cases < 10^6')
try:
cases = abs(int(sys.argv[1]))
results = simulation(cases)
except:
results = simulation()
# analysis
results['first']['gold_percent'] = 100 * results['first']['gold'] / results['cases']
results['second']['gold_percent'] = 100 * results['second']['gold'] / results['first']['gold']
results['second']['silver_percent'] = 100 * results['second']['silver'] / results['first']['gold']
print('Complete!\n')
print('\tOut of {r[cases]} test cases, in {r[first][gold]} cases (~{r[first][gold_percent]}%) where the first coin was gold:'.format(r=results))
print('\tThe second coin was gold in {r[second][gold]} cases (~{r[second][gold_percent]}%).'.format(r=results))
print('\tThe second coin was silver in {r[second][silver]} cases (~{r[second][silver_percent]}%).'.format(r=results))
print('\nQuitting...\n')
if __name__ == '__main__':
main()