Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ca60090

Browse files
authoredJun 14, 2024··
Update sum_of_outcomes_for_rolling_n_sided_dice_k_time.py
1 parent 1b41f73 commit ca60090

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed
 

‎maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
21
import numpy as np
32

43
def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list:
54
"""
65
Sum of outcomes for rolling an N-sided dice K times.
7-
6+
87
This function returns a list. The first element is an array.
98
That array contains indexes.
109
The second element is another array.
1110
That array contains probabilities for getting each index values
1211
as the sum of obtained side values.
1312
1413
Algorithm Explanation:
15-
14+
1615
1. Explanation of range:
1716
When we are rolling a six-sided dice the range becomes
1817
1 to 6.
@@ -22,28 +21,28 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list:
2221
1 is the minimum and 6 is the maximum of side values
2322
for a 6 sided dice. Therefore, the range is 5 to 30.
2423
Therefore, the range is k to n*k.
25-
24+
2625
2. Explanation of probability distribution:
2726
Say we are rolling a six-sided dice 2 times.
2827
for 0 roll, the outcome is 0 with probability 1.
2928
For the first roll, the outcome is 1 to 6 equally distributed.
30-
29+
3130
For the second roll, each previous outcome (1-6) will face
3231
an addition from the second rolling (1-6).
3332
If the first outcome is (known) 3, then the probability of
3433
getting each of 4 to 9 will be 1/6.
35-
34+
3635
The sum becomes 2 for two 1 outcomes. But the sum becomes
3736
3 for two outcomes (1,2) and (2,1).
38-
37+
3938
Link:
4039
https://www.thoughtco.com/
4140
probabilities-of-rolling-two-dice-3126559
42-
41+
4342
That phenomenon is the same as the convolution. However, the
4443
index position is different. Therefore, we adjust the index.
45-
46-
44+
45+
4746
NB: a) We are assuming a fair dice
4847
b) Bernoulli's theory works with getting the probability of
4948
exactly 3 sixes while rolling 5 times. It does not work directly
@@ -52,7 +51,7 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list:
5251
is more computationally extensive.
5352
c) The algorithm can be used in playing games where the sum of
5453
multiple dice throwing is needed.
55-
54+
5655
I used that method in my paper to draw the distribution
5756
Titled: Uncertainty-aware Decisions in Cloud Computing:
5857
Foundations and Future Directions
@@ -79,9 +78,9 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list:
7978
([2, 3, 4], [0.25, 0.5, 0.25])
8079
>>> outcome_of_rolling_n_sided_dice_k_time(2,4)
8180
([4, 5, 6, 7, 8], [0.0625, 0.25, 0.375, 0.25, 0.0625])
82-
81+
8382
"""
84-
83+
8584
if n_side != int(n_side) or k_time != int(k_time):
8685
raise ValueError("The function only accepts integer values")
8786
if n_side < 2:
@@ -90,18 +89,17 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list:
9089
raise ValueError("Roll count should be more than 0")
9190
if k_time > 100 or n_side > 100:
9291
raise ValueError("Limited to 100 sides or rolling to avoid memory issues")
93-
94-
probability_distribution = 1
95-
distribution_step = np.ones(n_side)/n_side
96-
92+
93+
prob_dist = 1
94+
dist_step = np.ones(n_side)/n_side
95+
9796
iter1 = 0
9897
while iter1 < k_time:
99-
probability_distribution =
100-
np.convolve(probability_distribution, distribution_step)
98+
prob_dist = np.convolve(prob_dist, dist_step)
10199
iter1 = iter1+1
102-
100+
103101
index = list(range(k_time,k_time*n_side+1))
104-
return index, list(probability_distribution)
102+
return index, list(prob_dist)
105103

106104

107105
'''

0 commit comments

Comments
 (0)
Please sign in to comment.