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(2,2)
78
- ([2, 3, 4], array([0.25, 0.5 , 0.25]))
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
+
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(2,2)
79
+ ([2, 3, 4], array([0.25, 0.5 , 0.25]))
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
probability_distribution = 1
92
- distribution_step = np .ones (n_side )/ n_side
93
-
93
+ distribution_step = np .ones (n_side ) / n_side
94
+
94
95
iter1 = 0
95
96
while iter1 < k_time :
96
- probability_distribution = np .convolve (probability_distribution , distribution_step )
97
- iter1 = iter1 + 1
98
-
99
- index = list (range (k_time ,k_time * n_side + 1 ))
97
+ probability_distribution = np .convolve (
98
+ probability_distribution , distribution_step
99
+ )
100
+ iter1 = iter1 + 1
101
+
102
+ index = list (range (k_time , k_time * n_side + 1 ))
100
103
return index , list (probability_distribution )
101
104
102
105
103
- '''
106
+ """
104
107
# Extra code for the verification
105
108
106
109
index_dist = outcome_of_rolling_n_sided_dice_k_time(6, 5)
@@ -111,4 +114,4 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list:
111
114
import matplotlib.pyplot as plt
112
115
plt.bar(index_dist[0], index_dist[1])
113
116
114
- '''
117
+ """
0 commit comments