Skip to content

Commit df400a7

Browse files
authored
Update probability_of_n_heads_in_m_tossing.py
1 parent 707b446 commit df400a7

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

maths/probability_of_n_heads_in_m_tossing.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
The probability of getting exactly n heads in m tossing.
3+
24
Algorithm Explanation:
35
If you toss 0 time -> 0 head
46
Distribution [1] -> Meaning: 1 in the 0-index
@@ -26,7 +28,7 @@
2628
Journal: ACM Computing Surveys (CSUR)
2729
"""
2830

29-
import numpy
31+
import numpy as np
3032

3133

3234
def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int:
@@ -63,10 +65,12 @@ def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int
6365
if head_count > toss_count:
6466
raise ValueError("Head count should be smaller than toss count")
6567

66-
value = numpy.ones(1)
67-
68-
for iter1 in range(toss_count):
69-
value = numpy.append(value, [0], axis=0) + numpy.append([0], value, axis=0)
68+
value = np.ones(1)
69+
70+
iter1 = 0
71+
while iter1 < toss_count :
72+
value = np.append(value, [0], axis=0) + np.append([0], value, axis=0)
7073
value = value / 2
74+
iter1 = iter1 +1
7175

7276
return value[head_count]

0 commit comments

Comments
 (0)