1
1
import numpy as np
2
2
3
+
3
4
def outcome_of_rolling_n_sided_dice_k_time (n_side : int , k_time : int ) -> list :
4
5
"""
5
- Sum of outcomes for rolling an N-sided dice K times.
6
-
7
- This function returns a list. The first element is an array.
8
- That array contains indexes.
9
- The second element is another array.
10
- That array contains probabilities for getting each index values
11
- as the sum of obtained side values.
12
-
13
- Algorithm Explanation:
14
-
15
- 1. Explanation of range:
16
- When we are rolling a six-sided dice the range becomes
17
- 1 to 6.
18
- While rolling 5 times range becomes 2 to 12.
19
- The sum outcomes becomes 5 when all rolling finds 1.
20
- 30 happens when all rolling finds 6.
21
- 1 is the minimum and 6 is the maximum of side values
22
- for a 6 sided dice. Therefore, the range is 5 to 30.
23
- Therefore, the range is k to n*k.
24
-
25
- 2. Explanation of probability distribution:
26
- Say we are rolling a six-sided dice 2 times.
27
- for 0 roll, the outcome is 0 with probability 1.
28
- For the first roll, the outcome is 1 to 6 equally distributed.
29
-
30
- For the second roll, each previous outcome (1-6) will face
31
- an addition from the second rolling (1-6).
32
- If the first outcome is (known) 3, then the probability of
33
- getting each of 4 to 9 will be 1/6.
34
-
35
- The sum becomes 2 for two 1 outcomes. But the sum becomes
36
- 3 for two outcomes (1,2) and (2,1).
37
-
38
- Link:
39
- https://www.thoughtco.com/
40
- probabilities-of-rolling-two-dice-3126559
41
-
42
- That phenomenon is the same as the convolution. However, the
43
- index position is different. Therefore, we adjust the index.
44
-
45
-
46
- NB: a) We are assuming a fair dice
47
- b) Bernoulli's theory works with getting the probability of
48
- exactly 3 sixes while rolling 5 times. It does not work directly
49
- with the sum. The same sum can come in many combinations.
50
- Finding all of those combinations and applying Bernoulli
51
- is more computationally extensive.
52
- c) The algorithm can be used in playing games where the sum of
53
- multiple dice throwing is needed.
54
-
55
- I used that method in my paper to draw the distribution
56
- Titled: Uncertainty-aware Decisions in Cloud Computing:
57
- Foundations and Future Directions
58
- Journal: ACM Computing Surveys (CSUR)
59
- link: https://dl.acm.org/doi/abs/10.1145/3447583
60
- The PDF version of the paper is available on Google Scholar.
61
-
62
-
63
- >>> import numpy as np
64
- >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5)
65
- Traceback (most recent call last):
66
- ...
67
- ValueError: The function only accepts integer values
68
- >>> outcome_of_rolling_n_sided_dice_k_time(-1,5)
69
- Traceback (most recent call last):
70
- ...
71
- ValueError: Side count should be more than 1
72
- >>> outcome_of_rolling_n_sided_dice_k_time(3,-2)
73
- Traceback (most recent call last):
74
- ...
75
- ValueError: Roll count should be more than 0
76
-
77
- >>> outcome_of_rolling_n_sided_dice_k_time(4,2)
78
- [[2, 3, 4, 5, 6, 7, 8],
79
- array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])]
80
- >>> outcome_of_rolling_n_sided_dice_k_time(6,2)
81
- [[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
82
- array([0.02777778, 0.05555556, 0.08333333, 0.11111111, 0.13888889,
83
- 0.16666667, 0.13888889, 0.11111111, 0.08333333, 0.05555556,
84
- 0.02777778])]
85
- >>> outcome_of_rolling_n_sided_dice_k_time(6,3)
86
- [[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18],
87
- array([0.00462963, 0.01388889, 0.02777778, 0.0462963 , 0.06944444,
88
- 0.09722222, 0.11574074, 0.125 , 0.125 , 0.11574074,
89
- 0.09722222, 0.06944444, 0.0462963 , 0.02777778, 0.01388889,
90
- 0.00462963])]
6
+ Sum of outcomes for rolling an N-sided dice K times.
7
+
8
+ This function returns a list. The first element is an array.
9
+ That array contains indexes.
10
+ The second element is another array.
11
+ That array contains probabilities for getting each index values
12
+ as the sum of obtained side values.
13
+
14
+ Algorithm Explanation:
15
+
16
+ 1. Explanation of range:
17
+ When we are rolling a six-sided dice the range becomes
18
+ 1 to 6.
19
+ While rolling 5 times range becomes 2 to 12.
20
+ The sum outcomes becomes 5 when all rolling finds 1.
21
+ 30 happens when all rolling finds 6.
22
+ 1 is the minimum and 6 is the maximum of side values
23
+ for a 6 sided dice. Therefore, the range is 5 to 30.
24
+ Therefore, the range is k to n*k.
25
+
26
+ 2. Explanation of probability distribution:
27
+ Say we are rolling a six-sided dice 2 times.
28
+ for 0 roll, the outcome is 0 with probability 1.
29
+ For the first roll, the outcome is 1 to 6 equally distributed.
30
+
31
+ For the second roll, each previous outcome (1-6) will face
32
+ an addition from the second rolling (1-6).
33
+ If the first outcome is (known) 3, then the probability of
34
+ getting each of 4 to 9 will be 1/6.
35
+
36
+ The sum becomes 2 for two 1 outcomes. But the sum becomes
37
+ 3 for two outcomes (1,2) and (2,1).
38
+
39
+ Link:
40
+ https://www.thoughtco.com/
41
+ probabilities-of-rolling-two-dice-3126559
42
+
43
+ That phenomenon is the same as the convolution. However, the
44
+ index position is different. Therefore, we adjust the index.
45
+
46
+
47
+ NB: a) We are assuming a fair dice
48
+ b) Bernoulli's theory works with getting the probability of
49
+ exactly 3 sixes while rolling 5 times. It does not work directly
50
+ with the sum. The same sum can come in many combinations.
51
+ Finding all of those combinations and applying Bernoulli
52
+ is more computationally extensive.
53
+ c) The algorithm can be used in playing games where the sum of
54
+ multiple dice throwing is needed.
55
+
56
+ I used that method in my paper to draw the distribution
57
+ Titled: Uncertainty-aware Decisions in Cloud Computing:
58
+ Foundations and Future Directions
59
+ Journal: ACM Computing Surveys (CSUR)
60
+ link: https://dl.acm.org/doi/abs/10.1145/3447583
61
+ The PDF version of the paper is available on Google Scholar.
62
+
63
+
64
+ >>> import numpy as np
65
+ >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5)
66
+ Traceback (most recent call last):
67
+ ...
68
+ ValueError: The function only accepts integer values
69
+ >>> outcome_of_rolling_n_sided_dice_k_time(-1,5)
70
+ Traceback (most recent call last):
71
+ ...
72
+ ValueError: Side count should be more than 1
73
+ >>> outcome_of_rolling_n_sided_dice_k_time(3,-2)
74
+ Traceback (most recent call last):
75
+ ...
76
+ ValueError: Roll count should be more than 0
77
+
78
+ >>> outcome_of_rolling_n_sided_dice_k_time(4,2)
79
+ [[2, 3, 4, 5, 6, 7, 8],
80
+ array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])]
81
+ >>> outcome_of_rolling_n_sided_dice_k_time(6,2)
82
+ [[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
83
+ array([0.02777778, 0.05555556, 0.08333333, 0.11111111, 0.13888889,
84
+ 0.16666667, 0.13888889, 0.11111111, 0.08333333, 0.05555556,
85
+ 0.02777778])]
86
+ >>> outcome_of_rolling_n_sided_dice_k_time(6,3)
87
+ [[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18],
88
+ array([0.00462963, 0.01388889, 0.02777778, 0.0462963 , 0.06944444,
89
+ 0.09722222, 0.11574074, 0.125 , 0.125 , 0.11574074,
90
+ 0.09722222, 0.06944444, 0.0462963 , 0.02777778, 0.01388889,
91
+ 0.00462963])]
91
92
"""
92
-
93
+
93
94
if n_side != int (n_side ) or k_time != int (k_time ):
94
95
raise ValueError ("The function only accepts integer values" )
95
96
if n_side < 2 :
96
97
raise ValueError ("Side count should be more than 1" )
97
- if k_time < 1 :
98
+ if k_time < 1 :
98
99
raise ValueError ("Roll count should be more than 0" )
99
100
if k_time > 100 or n_side > 100 :
100
101
raise ValueError ("Limited to 100 sides or rolling to avoid memory issues" )
101
-
102
+
102
103
probability_distribution = 1
103
- distribution_step = np .ones (n_side )/ n_side
104
-
104
+ distribution_step = np .ones (n_side ) / n_side
105
+
105
106
iter1 = 0
106
107
while iter1 < k_time :
107
- probability_distribution = np .convolve (probability_distribution , distribution_step )
108
- iter1 = iter1 + 1
109
-
108
+ probability_distribution = np .convolve (
109
+ probability_distribution , distribution_step
110
+ )
111
+ iter1 = iter1 + 1
112
+
110
113
output = []
111
- index = list (range (k_time ,k_time * n_side + 1 ))
114
+ index = list (range (k_time , k_time * n_side + 1 ))
112
115
output .append (index )
113
116
output .append (probability_distribution )
114
117
return output
115
118
116
119
117
- '''
120
+ """
118
121
# Extra code for the verification
119
122
120
123
index_dist = outcome_of_rolling_n_sided_dice_k_time(6, 5)
@@ -125,4 +128,4 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list:
125
128
import matplotlib.pyplot as plt
126
129
plt.bar(index_dist[0], index_dist[1])
127
130
128
- '''
131
+ """
0 commit comments