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