-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo.py
44 lines (21 loc) · 742 Bytes
/
geo.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
'''
Geometric distribution :
keep trying till we get a success after X number of bernoulli trials
so X : proba that an event will take X bernoulli trials to happen
'''
import numpy as np
def geom(p):
x = 1
if (p<0) or (p>1):
raise ValueError ('Probability must be between 0 and 1')
while(True):
#generate a random number
random_number = np.random.random()
if random_number < p :
return x
else:
x += 1
#lets test this with a free throws example (basketball)
#lets say a player has 30% chance of getting a perfect free throw
shotsBeforePerfectShot = geom(0.3)
print(str(shotsBeforePerfectShot) + ' Fts before perfect one')