forked from andrewrgarcia/monte-carlo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmclotstrats.py
71 lines (56 loc) · 1.95 KB
/
mclotstrats.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
71
'''mclotstrats.py: A simple Monte Carlo algorithm for lottery games
Developed using Pythonista for iPhone/iPad
(c) Andrew R Garcia, 2017
*Lottery customization: no. of balls (balls) in lottery; lowest (low) and highest (high) numbers in lottery
*Game customization: number of lotteries played (N); number of tickets played (ticketN) per N lottery
'''
import random
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from t4exer import factorial as fac
#N=int(fac(49)/(fac(6)*fac(49-6)))
def mcG(N=100,balls=3,low=1,high=49,ticketN=1):
#developer: Andrew Garcia
#with plt.xkcd():
x=np.linspace(0,N,N)
# L_draw=sample(balls,low,high)
ballpop=range(low,high+1)
ticket=np.zeros([ticketN,balls])
ticketcorr=np.zeros([ticketN,balls])
for p in range(0,ticketN):
ticket[p]=random.sample(ballpop,balls)
# lottery numbers for tickets bought
ticketcorr[p]=np.sort(ticket[p])
sum_winners=np.zeros(N)
for j in range(0,N):
# player_game=sample(balls,low,high)
winners=np.zeros(ticketN)
ballpop=range(low,high+1)
L_draw=random.sample(ballpop,balls)
L_drawsort=np.sort(L_draw)
print('lottery draw #{}: {}'.format(j+1,L_draw))
for i in range(0,ticketN):
if np.array_equal(ticketcorr[i],L_drawsort)==True:
winners[i]=1
else:
winners[i]=0
sum_winners[j]=np.sum(winners)
grandsum_winners=np.sum(sum_winners)
print ('____________________________________________________')
print ('number of times played: ', N)
print ('tickets bought per played lottery: ', ticketN)
print ('winning tickets: ',ticket)
print ('lottery winning timeline: ',sum_winners)
print ('# times won: ',grandsum_winners)
print ('investment for $1 lottery: $', N*ticketN*1)
plt.ylabel("winning tickets")
plt.xlim(-0.2,N+0.2)
#plt.ylim(-0.2)
plt.xlabel("Lottery timeline")
plt.title("Lottery Draw")
plt.plot(x,sum_winners,'o',color='magenta')
plt.show()
# plt.clf()
# plt.cla()
# plt.close()