-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseball.py
54 lines (36 loc) · 1.1 KB
/
baseball.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
import random
from multiprocessing import Pool
num_threads = 16
num_seasons = 100000
avgs = [0] *num_threads
def compute(num_seasons):
print "Starting thread for num_seasons", num_seasons
al_east = {"Yankees":0, "Sox":0, "Jays":0, "Rays":0, "Orioles":0}
avg = {"Yankees":0.0, "Sox":0.0, "Jays":0.0, "Rays":0.0, "Orioles":0.0}
first_avg = 0.0
for j in range(num_seasons):
for i in range(0,162):
for team in al_east:
al_east[team] += random.randint(0,1)
first = 0.0
for team in avg:
avg[team] += al_east[team]
if al_east[team] > first:
first = al_east[team]
al_east[team] = 0
first_avg += first
for team in avg:
avg[team] = avg[team]/num_seasons
first_avg /= num_seasons
return first_avg
pool = Pool(processes = num_threads)
threads = []
print "spinning up threads"
avgs = pool.map(compute, [num_seasons] * num_threads)
print "joining threads"
pool.close()
pool.join()
first_avg = 0.0
for avg in avgs:
first_avg += avg
print first_avg/num_threads