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