forked from garyexplains/examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
voncoin.py
50 lines (43 loc) · 983 Bytes
/
voncoin.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
# Example of John von Neumann fair coin
import random
def biasedcoin():
b = random.randint(1,10)
if b > 7:
return 1
else:
return 0
# This works because the chance of getting a BIASED result and then an UNBIASED result
# is the same as getting an UNBIASED result and then a BIASED result
# 50/50
def unbiasedcoin():
while True:
b1 = biasedcoin()
b2 = biasedcoin()
if b1 == 1 and b2 == 0:
return 0
if b1 == 0 and b2 == 1:
return 1
# BIASED
print("BIASED")
count0 = 0
count1 = 0
for x in range(1000):
b = biasedcoin()
if b == 1:
count1 = count1 + 1
else:
count0 = count0 + 1
print("Count0 is", count0)
print("Count1 is", count1)
#UNBIASED
print("UNBIASED")
count0 = 0
count1 = 0
for x in range(1000):
b = unbiasedcoin()
if b == 1:
count1 = count1 + 1
else:
count0 = count0 + 1
print("Count0 is", count0)
print("Count1 is", count1)