3
3
def outcome_of_rolling_n_sided_dice_k_time (n_side : int , k_time : int ) -> list :
4
4
"""
5
5
Sum of outcomes for rolling an N-sided dice K times.
6
-
6
+
7
7
This function returns a list. The first element is an array.
8
8
That array contains indexes.
9
9
The second element is another array.
10
10
That array contains probabilities for getting each index values
11
11
as the sum of obtained side values.
12
12
13
13
Algorithm Explanation:
14
-
14
+
15
15
1. Explanation of range:
16
16
When we are rolling a six-sided dice the range becomes
17
17
1 to 6.
@@ -21,28 +21,28 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list:
21
21
1 is the minimum and 6 is the maximum of side values
22
22
for a 6 sided dice. Therefore, the range is 5 to 30.
23
23
Therefore, the range is k to n*k.
24
-
24
+
25
25
2. Explanation of probability distribution:
26
26
Say we are rolling a six-sided dice 2 times.
27
27
for 0 roll, the outcome is 0 with probability 1.
28
28
For the first roll, the outcome is 1 to 6 equally distributed.
29
-
29
+
30
30
For the second roll, each previous outcome (1-6) will face
31
31
an addition from the second rolling (1-6).
32
32
If the first outcome is (known) 3, then the probability of
33
33
getting each of 4 to 9 will be 1/6.
34
-
34
+
35
35
The sum becomes 2 for two 1 outcomes. But the sum becomes
36
36
3 for two outcomes (1,2) and (2,1).
37
-
37
+
38
38
Link:
39
39
https://www.thoughtco.com/
40
40
probabilities-of-rolling-two-dice-3126559
41
-
41
+
42
42
That phenomenon is the same as the convolution. However, the
43
43
index position is different. Therefore, we adjust the index.
44
-
45
-
44
+
45
+
46
46
NB: a) We are assuming a fair dice
47
47
b) Bernoulli's theory works with getting the probability of
48
48
exactly 3 sixes while rolling 5 times. It does not work directly
@@ -51,7 +51,7 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list:
51
51
is more computationally extensive.
52
52
c) The algorithm can be used in playing games where the sum of
53
53
multiple dice throwing is needed.
54
-
54
+
55
55
I used that method in my paper to draw the distribution
56
56
Titled: Uncertainty-aware Decisions in Cloud Computing:
57
57
Foundations and Future Directions
@@ -78,28 +78,28 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list:
78
78
([2, 3, 4], [0.25, 0.5, 0.25])
79
79
>>> outcome_of_rolling_n_sided_dice_k_time(2,4)
80
80
([4, 5, 6, 7, 8], [0.0625, 0.25, 0.375, 0.25, 0.0625])
81
-
81
+
82
82
"""
83
-
83
+
84
84
if n_side != int (n_side ) or k_time != int (k_time ):
85
85
raise ValueError ("The function only accepts integer values" )
86
86
if n_side < 2 :
87
87
raise ValueError ("Side count should be more than 1" )
88
88
if k_time < 1 :
89
89
raise ValueError ("Roll count should be more than 0" )
90
90
if k_time > 100 or n_side > 100 :
91
- raise ValueError ("Limited to 100 sides or rolling to
91
+ raise ValueError ("Limited to 100 sides or rolling to
92
92
avoid memory issues ")
93
-
93
+
94
94
probability_distribution = 1
95
95
distribution_step = np .ones (n_side )/ n_side
96
-
96
+
97
97
iter1 = 0
98
98
while iter1 < k_time :
99
99
probability_distribution =
100
100
np .convolve (probability_distribution , distribution_step )
101
101
iter1 = iter1 + 1
102
-
102
+
103
103
index = list (range (k_time ,k_time * n_side + 1 ))
104
104
return index , list (probability_distribution )
105
105
0 commit comments