Skip to content

Commit f2d459a

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

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list:
44
"""
55
Sum of outcomes for rolling an N-sided dice K times.
6-
6+
77
This function returns a list. The first element is an array.
88
That array contains indexes.
99
The second element is another array.
1010
That array contains probabilities for getting each index values
1111
as the sum of obtained side values.
1212
1313
Algorithm Explanation:
14-
14+
1515
1. Explanation of range:
1616
When we are rolling a six-sided dice the range becomes
1717
1 to 6.
@@ -21,28 +21,28 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list:
2121
1 is the minimum and 6 is the maximum of side values
2222
for a 6 sided dice. Therefore, the range is 5 to 30.
2323
Therefore, the range is k to n*k.
24-
24+
2525
2. Explanation of probability distribution:
2626
Say we are rolling a six-sided dice 2 times.
2727
for 0 roll, the outcome is 0 with probability 1.
2828
For the first roll, the outcome is 1 to 6 equally distributed.
29-
29+
3030
For the second roll, each previous outcome (1-6) will face
3131
an addition from the second rolling (1-6).
3232
If the first outcome is (known) 3, then the probability of
3333
getting each of 4 to 9 will be 1/6.
34-
34+
3535
The sum becomes 2 for two 1 outcomes. But the sum becomes
3636
3 for two outcomes (1,2) and (2,1).
37-
37+
3838
Link:
3939
https://www.thoughtco.com/
4040
probabilities-of-rolling-two-dice-3126559
41-
41+
4242
That phenomenon is the same as the convolution. However, the
4343
index position is different. Therefore, we adjust the index.
44-
45-
44+
45+
4646
NB: a) We are assuming a fair dice
4747
b) Bernoulli's theory works with getting the probability of
4848
exactly 3 sixes while rolling 5 times. It does not work directly
@@ -51,7 +51,7 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list:
5151
is more computationally extensive.
5252
c) The algorithm can be used in playing games where the sum of
5353
multiple dice throwing is needed.
54-
54+
5555
I used that method in my paper to draw the distribution
5656
Titled: Uncertainty-aware Decisions in Cloud Computing:
5757
Foundations and Future Directions
@@ -78,28 +78,28 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list:
7878
([2, 3, 4], [0.25, 0.5, 0.25])
7979
>>> outcome_of_rolling_n_sided_dice_k_time(2,4)
8080
([4, 5, 6, 7, 8], [0.0625, 0.25, 0.375, 0.25, 0.0625])
81-
81+
8282
"""
83-
83+
8484
if n_side != int(n_side) or k_time != int(k_time):
8585
raise ValueError("The function only accepts integer values")
8686
if n_side < 2:
8787
raise ValueError("Side count should be more than 1")
8888
if k_time < 1:
8989
raise ValueError("Roll count should be more than 0")
9090
if k_time > 100 or n_side > 100:
91-
raise ValueError("Limited to 100 sides or rolling to
91+
raise ValueError("Limited to 100 sides or rolling to
9292
avoid memory issues")
93-
93+
9494
probability_distribution = 1
9595
distribution_step = np.ones(n_side)/n_side
96-
96+
9797
iter1 = 0
9898
while iter1 < k_time:
9999
probability_distribution =
100100
np.convolve(probability_distribution, distribution_step)
101101
iter1 = iter1+1
102-
102+
103103
index = list(range(k_time,k_time*n_side+1))
104104
return index, list(probability_distribution)
105105

0 commit comments

Comments
 (0)