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/probabilities-of-rolling-two-dice-3126559
40
-
41
- That phenomenon is the same as the convolution. However, the index
42
- position is different. Therefore, we adjust the index.
43
-
44
-
45
- NB: a) We are assuming a fair dice
46
- b) Bernoulli's theory works with getting the probability of
47
- exactly 3 sixes while rolling 5 times. It does not work directly
48
- with the sum. The same sum can come in many combinations.
49
- Finding all of those combinations and applying Bernoulli
50
- is more computationally extensive.
51
- c) The algorithm can be used in playing games where the sum of
52
- multiple dice throwing is needed.
53
-
54
- I used that method in my paper to draw the distribution
55
- Titled: Uncertainty-aware Decisions in Cloud Computing:
56
- Foundations and Future Directions
57
- Journal: ACM Computing Surveys (CSUR)
58
- link: https://dl.acm.org/doi/abs/10.1145/3447583
59
- The PDF version of the paper is available on Google Scholar.
60
-
61
-
62
- >>> import numpy as np
63
- >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5)
64
- Traceback (most recent call last):
65
- ...
66
- ValueError: The function only accepts integer values
67
- >>> outcome_of_rolling_n_sided_dice_k_time(-1,5)
68
- Traceback (most recent call last):
69
- ...
70
- ValueError: Side count should be more than 1
71
- >>> outcome_of_rolling_n_sided_dice_k_time(3,-2)
72
- Traceback (most recent call last):
73
- ...
74
- ValueError: Roll count should be more than 0
75
-
76
- >>> outcome_of_rolling_n_sided_dice_k_time(4,2)
77
- [range(2, 9), array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])]
78
- >>> outcome_of_rolling_n_sided_dice_k_time(6,2)
79
- [range(2, 13),
80
- array([0.02777778, 0.05555556, 0.08333333, 0.11111111, 0.13888889,
81
- 0.16666667, 0.13888889, 0.11111111, 0.08333333, 0.05555556,
82
- 0.02777778])]
83
- >>> outcome_of_rolling_n_sided_dice_k_time(6,3)
84
- [range(3, 19),
85
- array([0.00462963, 0.01388889, 0.02777778, 0.0462963 , 0.06944444,
86
- 0.09722222, 0.11574074, 0.125 , 0.125 , 0.11574074,
87
- 0.09722222, 0.06944444, 0.0462963 , 0.02777778, 0.01388889,
88
- 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/probabilities-of-rolling-two-dice-3126559
41
+
42
+ That phenomenon is the same as the convolution. However, the index
43
+ 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
+ [range(2, 9), array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])]
79
+ >>> outcome_of_rolling_n_sided_dice_k_time(6,2)
80
+ [range(2, 13),
81
+ array([0.02777778, 0.05555556, 0.08333333, 0.11111111, 0.13888889,
82
+ 0.16666667, 0.13888889, 0.11111111, 0.08333333, 0.05555556,
83
+ 0.02777778])]
84
+ >>> outcome_of_rolling_n_sided_dice_k_time(6,3)
85
+ [range(3, 19),
86
+ array([0.00462963, 0.01388889, 0.02777778, 0.0462963 , 0.06944444,
87
+ 0.09722222, 0.11574074, 0.125 , 0.125 , 0.11574074,
88
+ 0.09722222, 0.06944444, 0.0462963 , 0.02777778, 0.01388889,
89
+ 0.00462963])]
89
90
"""
90
-
91
+
91
92
if n_side != int (n_side ) or k_time != int (k_time ):
92
93
raise ValueError ("The function only accepts integer values" )
93
94
if n_side < 2 :
94
95
raise ValueError ("Side count should be more than 1" )
95
- if k_time < 1 :
96
+ if k_time < 1 :
96
97
raise ValueError ("Roll count should be more than 0" )
97
98
if k_time > 100 or n_side > 100 :
98
99
raise ValueError ("Limited to 100 sides or rolling to avoid memory issues" )
99
-
100
+
100
101
probability_distribution = 1
101
- distribution_step = np .ones (n_side )/ n_side
102
-
102
+ distribution_step = np .ones (n_side ) / n_side
103
+
103
104
iter1 = 0
104
105
while iter1 < k_time :
105
- probability_distribution = np .convolve (probability_distribution , distribution_step )
106
- iter1 = iter1 + 1
107
-
106
+ probability_distribution = np .convolve (
107
+ probability_distribution , distribution_step
108
+ )
109
+ iter1 = iter1 + 1
110
+
108
111
output = []
109
- index = range (k_time ,k_time * n_side + 1 )
112
+ index = range (k_time , k_time * n_side + 1 )
110
113
output .append (index )
111
114
output .append (probability_distribution )
112
115
return output
113
116
114
117
115
- '''
118
+ """
116
119
# Extra code for the verification
117
120
118
121
index_dist = outcome_of_rolling_n_sided_dice_k_time(6, 5)
@@ -123,4 +126,4 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list:
123
126
import matplotlib.pyplot as plt
124
127
plt.bar(index_dist[0], index_dist[1])
125
128
126
- '''
129
+ """
0 commit comments