Skip to content

Commit f348aa0

Browse files
authored
Update sum_of_outcomes_for_rolling_n_sided_dice_k_time.py
1 parent 752ed1e commit f348aa0

1 file changed

Lines changed: 85 additions & 89 deletions

File tree

maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py

Lines changed: 85 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,87 @@
11
import numpy as np
22

33

4-
def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list:
4+
def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> float:
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+
array([0.25, 0.5 , 0.25, 2. , 4. ])
83+
>>> outcome_of_rolling_n_sided_dice_k_time(2,4)
84+
array([0.0625, 0.25 , 0.375 , 0.25 , 0.0625, 4. , 8. ])
8785
8886
"""
8987

@@ -103,24 +101,22 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list:
103101
while iter1 < k_time:
104102
prob_dist = np.convolve(prob_dist, dist_step)
105103
iter1 = iter1 + 1
104+
105+
prob_index = np.concatenate((prob_dist, np.array([k_time, k_time*n_side])))
106106

107-
prob_list = list(prob_dist)
108-
prob_list.append(k_time)
109-
prob_list.append(k_time * n_side)
110-
111-
return prob_list
107+
return prob_index
112108

113109

114110
"""
115111
# Extra code for the verification
116112
117113
dist_index = outcome_of_rolling_n_sided_dice_k_time(6, 3)
118114
119-
the_range = range(dist_index[-2], dist_index[-1]+1)
115+
the_range = range(int(dist_index[-2]), int(dist_index[-1]+1))
120116
probabilities = dist_index[:-2]
121117
print("Indexes:",the_range)
122118
123-
print("Distribution:",probabilities,"Summation:",np.sum(probabilities))
119+
print("Distribution:",probabilities, "Their summation:",np.sum(probabilities))
124120
125121
import matplotlib.pyplot as plt
126122
plt.bar(the_range, probabilities)

0 commit comments

Comments
 (0)