-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoinToss.py
70 lines (60 loc) · 2.07 KB
/
CoinToss.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
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 16 16:00:56 2019
@author: DannySwift
"""
import matplotlib.animation
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
def toss_coins(num_coins):
ar = np.random.binomial(1, 0.5, num_coins)
for i in range(1, len(ar)):
ar[i] = ar[i - 1] + ar[i]
df = pd.DataFrame(ar)
return(df)
def main():
coins = int(input("How many coins would you like to toss? "))
while coins != 0:
try:
df = df.merge(toss_coins(coins) + df.iloc[-1], how='outer')
except NameError:
df = toss_coins(coins)
df2 = df.copy()
for i in range(1, len(df)):
df2.iloc[i] = df.iloc[i] / (i + 1) * 100
plt.ylim(0, 100)
sns.lineplot(data=df2).set(ylabel='Percent Heads')
plt.show()
coins = int(input("How many more coins would you like to toss? "))
def main2():
num_coins = int(input("How many coins are there? "))
heads = int(input("How many are heads up? "))
coins = np.zeros(heads) + 1
coins = np.append(coins, np.zeros(num_coins - heads))
history = [coins.sum()]
flip = int(input("How many would you like to flip? "))
tosses = int(input("How many times? "))
tosses_per_frame = int(input("How many tosses per frame? "))
for toss in range(tosses):
ar = np.random.binomial(1, 0.5, flip)
which_coins = np.random.choice(num_coins, size=flip, replace=False)
for i in range(flip):
coins[which_coins[i]] = ar[i]
history.append(coins.sum())
df = pd.DataFrame(history)
fig = plt.figure()
plt.ylim(0, num_coins)
plt.xlim(0, tosses)
plt.ylabel('Heads')
plt.xlabel('Toss')
def animate(i):
p = sns.lineplot(data=df[:i * tosses_per_frame], legend=False)
plt.setp(p.lines, color='r')
ani = matplotlib.animation.FuncAnimation(fig, animate,
frames=int(tosses / tosses_per_frame) + 2)
plt.show()
if __name__ == '__main__':
main2()