Skip to content

Commit 752ed1e

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent f618200 commit 752ed1e

File tree

1 file changed

+83
-83
lines changed

1 file changed

+83
-83
lines changed

maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py

+83-83
Original file line numberDiff line numberDiff line change
@@ -3,87 +3,87 @@
33

44
def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list:
55
"""
6-
The sum of outcomes for rolling an N-sided dice K times.
7-
8-
This function returns a list. The last two elements are the
9-
range of probability distribution.
10-
The range is: 'k_time' to 'k_time*n_side'
11-
12-
Other elements contain probabilities for getting a summation
13-
from 'k_time' to 'k_time*n_side'.
14-
15-
Algorithm Explanation:
16-
17-
1. Explanation of range:
18-
When we are rolling a six-sided dice the range becomes
19-
1 to 6.
20-
While rolling 5 times range becomes 5 to 30.
21-
The sum outcomes become 5 when all rolling finds 1.
22-
30 happens when all rolling finds 6.
23-
1 is the minimum and 6 is the maximum of side values
24-
for a 6 sided dice. Therefore, the range is 5 to 30.
25-
Therefore, the range is k to n*k.
26-
27-
2. Explanation of probability distribution:
28-
Say we are rolling a six-sided dice 2 times.
29-
for 0 roll, the outcome is 0 with probability 1.
30-
For the first roll, the outcome is 1 to 6 equally distributed.
31-
32-
For the second roll, each previous outcome (1-6) will face
33-
an addition from the second rolling (1-6).
34-
If the first outcome is (known) 3, then the probability of
35-
getting each of 4 to 9 will be 1/6.
36-
37-
While rolling 2 dice simultaneously,
38-
the sum becomes 2 for two 1 outcomes. But the sum becomes
39-
3 for two different outcome combinations (1,2) and (2,1).
40-
The probability of getting 2 is 1/6.
41-
The probability of getting 3 is 2/6
42-
43-
Link to rolling two 6-sided dice combinations:
44-
https://www.thoughtco.com/
45-
probabilities-of-rolling-two-dice-3126559
46-
That phenomenon is the same as the convolution.
47-
48-
The algorithm can be used in playing games or solving
49-
problems where the sum of multiple dice throwing is needed.
50-
51-
52-
NB: a) We are assuming a fair dice
53-
b) Bernoulli's theory works with getting the probability of
54-
exactly 3 sixes while rolling 5 times. It does not work directly
55-
with the sum. The same sum can come in many combinations.
56-
Finding all of those combinations and applying Bernoulli
57-
is more computationally extensive.
58-
59-
I used that method in my paper to draw the distribution
60-
Titled: Uncertainty-aware Decisions in Cloud Computing:
61-
Foundations and Future Directions
62-
Journal: ACM Computing Surveys (CSUR)
63-
link: https://dl.acm.org/doi/abs/10.1145/3447583
64-
The PDF version of the paper is available on Google Scholar.
65-
66-
67-
>>> import numpy as np
68-
>>> outcome_of_rolling_n_sided_dice_k_time(.2,.5)
69-
Traceback (most recent call last):
70-
...
71-
ValueError: The function only accepts integer values
72-
>>> outcome_of_rolling_n_sided_dice_k_time(-1,5)
73-
Traceback (most recent call last):
74-
...
75-
ValueError: Side count should be more than 1
76-
>>> outcome_of_rolling_n_sided_dice_k_time(3,-2)
77-
Traceback (most recent call last):
78-
...
79-
ValueError: Roll count should be more than 0
80-
81-
>>> outcome_of_rolling_n_sided_dice_k_time(2,2)
82-
[0.25, 0.5, 0.25, 2, 4]
83-
>>> outcome_of_rolling_n_sided_dice_k_time(2,4)
84-
[0.0625, 0.25, 0.375, 0.25, 0.0625, 4, 8]
85-
>>> outcome_of_rolling_n_sided_dice_k_time(4,2)
86-
[0.0625, 0.125, 0.1875, 0.25, 0.1875, 0.125, 0.0625, 2, 8]
6+
The sum of outcomes for rolling an N-sided dice K times.
7+
8+
This function returns a list. The last two elements are the
9+
range of probability distribution.
10+
The range is: 'k_time' to 'k_time*n_side'
11+
12+
Other elements contain probabilities for getting a summation
13+
from 'k_time' to 'k_time*n_side'.
14+
15+
Algorithm Explanation:
16+
17+
1. Explanation of range:
18+
When we are rolling a six-sided dice the range becomes
19+
1 to 6.
20+
While rolling 5 times range becomes 5 to 30.
21+
The sum outcomes become 5 when all rolling finds 1.
22+
30 happens when all rolling finds 6.
23+
1 is the minimum and 6 is the maximum of side values
24+
for a 6 sided dice. Therefore, the range is 5 to 30.
25+
Therefore, the range is k to n*k.
26+
27+
2. Explanation of probability distribution:
28+
Say we are rolling a six-sided dice 2 times.
29+
for 0 roll, the outcome is 0 with probability 1.
30+
For the first roll, the outcome is 1 to 6 equally distributed.
31+
32+
For the second roll, each previous outcome (1-6) will face
33+
an addition from the second rolling (1-6).
34+
If the first outcome is (known) 3, then the probability of
35+
getting each of 4 to 9 will be 1/6.
36+
37+
While rolling 2 dice simultaneously,
38+
the sum becomes 2 for two 1 outcomes. But the sum becomes
39+
3 for two different outcome combinations (1,2) and (2,1).
40+
The probability of getting 2 is 1/6.
41+
The probability of getting 3 is 2/6
42+
43+
Link to rolling two 6-sided dice combinations:
44+
https://www.thoughtco.com/
45+
probabilities-of-rolling-two-dice-3126559
46+
That phenomenon is the same as the convolution.
47+
48+
The algorithm can be used in playing games or solving
49+
problems where the sum of multiple dice throwing is needed.
50+
51+
52+
NB: a) We are assuming a fair dice
53+
b) Bernoulli's theory works with getting the probability of
54+
exactly 3 sixes while rolling 5 times. It does not work directly
55+
with the sum. The same sum can come in many combinations.
56+
Finding all of those combinations and applying Bernoulli
57+
is more computationally extensive.
58+
59+
I used that method in my paper to draw the distribution
60+
Titled: Uncertainty-aware Decisions in Cloud Computing:
61+
Foundations and Future Directions
62+
Journal: ACM Computing Surveys (CSUR)
63+
link: https://dl.acm.org/doi/abs/10.1145/3447583
64+
The PDF version of the paper is available on Google Scholar.
65+
66+
67+
>>> import numpy as np
68+
>>> outcome_of_rolling_n_sided_dice_k_time(.2,.5)
69+
Traceback (most recent call last):
70+
...
71+
ValueError: The function only accepts integer values
72+
>>> outcome_of_rolling_n_sided_dice_k_time(-1,5)
73+
Traceback (most recent call last):
74+
...
75+
ValueError: Side count should be more than 1
76+
>>> outcome_of_rolling_n_sided_dice_k_time(3,-2)
77+
Traceback (most recent call last):
78+
...
79+
ValueError: Roll count should be more than 0
80+
81+
>>> outcome_of_rolling_n_sided_dice_k_time(2,2)
82+
[0.25, 0.5, 0.25, 2, 4]
83+
>>> outcome_of_rolling_n_sided_dice_k_time(2,4)
84+
[0.0625, 0.25, 0.375, 0.25, 0.0625, 4, 8]
85+
>>> outcome_of_rolling_n_sided_dice_k_time(4,2)
86+
[0.0625, 0.125, 0.1875, 0.25, 0.1875, 0.125, 0.0625, 2, 8]
8787
8888
"""
8989

@@ -103,10 +103,10 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list:
103103
while iter1 < k_time:
104104
prob_dist = np.convolve(prob_dist, dist_step)
105105
iter1 = iter1 + 1
106-
106+
107107
prob_list = list(prob_dist)
108108
prob_list.append(k_time)
109-
prob_list.append(k_time*n_side)
109+
prob_list.append(k_time * n_side)
110110

111111
return prob_list
112112

0 commit comments

Comments
 (0)