-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoin_flip_simulation.py
More file actions
52 lines (40 loc) · 1.07 KB
/
coin_flip_simulation.py
File metadata and controls
52 lines (40 loc) · 1.07 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
#! Python
# A program to simulate coin flips
from numpy import random
##user_number = int(input('How many times do you want to toss a coin?'))
head_count = []
tail_count = []
coin = ['head', 'tail']
##for i in range (0, user_number):
## a = random.choice(coin)
## if a == 'head':
## head_count.append(1)
## elif a == 'tail':
## tail_count.append(1)
##
##print('heads = ' + str(sum(head_count)))
##print('tails = ' + str(sum(tail_count)))
flip_coin_tries= []
def flip_coin():
j = 0
k = 1
count = 0
tries = 0
while j != k:
for i in range (0, 4):
a = random.choice(coin)
if a == 'head':
count += 1
if count == 4:
j = k
tries += 1
#print('heads ' + str(count) + '/4')
count = 0
#print('Tries = ' + str(tries))
percentage = 100 / tries
#print(percentage)
flip_coin_tries.append(percentage)
flip_coin_tries= []
for i in range(10000):
flip_coin()
print('% of getting 4/4 flips = ' + str(sum(flip_coin_tries) / 10000))