-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy patharrival.py
42 lines (32 loc) · 918 Bytes
/
arrival.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
import random
from collections import Counter
from matplotlib import pyplot as plt
from PIL import Image
import gif
random.seed(2020)
@gif.frame
def plot_arrival(count, count_last):
plt.figure(figsize=(5, 3), dpi=100)
plt.bar(count.keys(), count.values())
plt.bar(count_last.keys(), count_last.values())
plt.xlim(-1, 11)
plt.xticks(range(0, 10 + 1))
plt.ylim(0, 100)
def simulate_arrival(count, p=0.10):
if random.uniform(0, 1) <= p:
group = len(count)
else:
k = list(count.keys())
v = list(count.values())
group = random.choices(k, weights=v)[0]
return group
count = Counter({0})
count_last = count.copy()
frames = []
for _ in range(100):
group = simulate_arrival(count)
count.update({group})
frame = plot_arrival(count, count_last)
frames.append(frame)
count_last = count.copy()
gif.save(frames, "images/arrival.gif")