-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
The sum of outcomes for rolling an N-sided dice K times. #11452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
4fd289f
Create Image_Classification_Example_MNIST_CNN.py
dipuk0506 29671f5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4e3484a
Update and rename Image_Classification_Example_MNIST_CNN.py to image_…
dipuk0506 7b70f64
Update image_classification_example_mnist_cnn.py
dipuk0506 a4b7330
Delete computer_vision/image_classification_example_mnist_cnn.py
dipuk0506 158c1fe
Create probability_of_n_heads_in_m_tossing.py
dipuk0506 d8b68ba
Update probability_of_n_heads_in_m_tossing.py
dipuk0506 c49619c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b62a774
Update probability_of_n_heads_in_m_tossing.py
dipuk0506 707b446
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] df400a7
Update probability_of_n_heads_in_m_tossing.py
dipuk0506 929301c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b84ac59
Update probability_of_n_heads_in_m_tossing.py
dipuk0506 b79e0c1
Update probability_of_n_heads_in_m_tossing.py
dipuk0506 205cb87
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c987949
Update probability_of_n_heads_in_m_tossing.py
dipuk0506 366f1ff
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ac01ab0
Update probability_of_n_heads_in_m_tossing.py
dipuk0506 e4129b9
Update and rename probability_of_n_heads_in_m_tossing.py to outcome_o…
dipuk0506 00e2389
Update outcome_of_rolling_n_sided_dice_k_time.py
dipuk0506 ce9e835
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c7d9ea4
Update and rename outcome_of_rolling_n_sided_dice_k_time.py to sum_of…
dipuk0506 d8f511c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] fcf30bc
Update sum_of_outcomes_for_rolling_n_sided_dice_k_time.py
dipuk0506 360ef80
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 749499a
Update sum_of_outcomes_for_rolling_n_sided_dice_k_time.py
dipuk0506 7bea0d9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 164a51d
Update sum_of_outcomes_for_rolling_n_sided_dice_k_time.py
dipuk0506 b8cb3bc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ccf5053
Update sum_of_outcomes_for_rolling_n_sided_dice_k_time.py
dipuk0506 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import numpy as np | ||
|
||
def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int): | ||
""" | ||
Sum of outcomes for rolling an N-sided dice K times. | ||
|
||
This function returns a list. The first element is an array. | ||
That array contains indexes. | ||
The second element is another array. | ||
That array contains probabilities for getting each index values | ||
as the sum of obtained side values. | ||
|
||
Algorithm Explanation: | ||
|
||
1. Explanation of range: | ||
When we are rolling a six-sided dice the range becomes | ||
1 to 6. | ||
While rolling 5 times range becomes 2 to 12. | ||
The sum outcomes becomes 5 when all rolling finds 1. | ||
30 happens when all rolling finds 6. | ||
1 is the minimum and 6 is the maximum of side values | ||
for a 6 sided dice. Therefore, the range is 5 to 30. | ||
Therefore, the range is k to n*k. | ||
|
||
2. Explanation of probability distribution: | ||
Say we are rolling a six-sided dice 2 times. | ||
for 0 roll, the outcome is 0 with probability 1. | ||
For the first roll, the outcome is 1 to 6 equally distributed. | ||
|
||
For the second roll, each previous outcome (1-6) will face | ||
an addition from the second rolling (1-6). | ||
If the first outcome is (known) 3, then the probability of | ||
getting each of 4 to 9 will be 1/6. | ||
|
||
The sum becomes 2 for two 1 outcomes. But the sum becomes | ||
3 for two outcomes (1,2) and (2,1). | ||
|
||
Link: | ||
https://www.thoughtco.com/ | ||
probabilities-of-rolling-two-dice-3126559 | ||
|
||
That phenomenon is the same as the convolution. However, the | ||
index position is different. Therefore, we adjust the index. | ||
|
||
|
||
NB: a) We are assuming a fair dice | ||
b) Bernoulli's theory works with getting the probability of | ||
exactly 3 sixes while rolling 5 times. It does not work directly | ||
with the sum. The same sum can come in many combinations. | ||
Finding all of those combinations and applying Bernoulli | ||
is more computationally extensive. | ||
c) The algorithm can be used in playing games where the sum of | ||
multiple dice throwing is needed. | ||
|
||
I used that method in my paper to draw the distribution | ||
Titled: Uncertainty-aware Decisions in Cloud Computing: | ||
Foundations and Future Directions | ||
Journal: ACM Computing Surveys (CSUR) | ||
link: https://dl.acm.org/doi/abs/10.1145/3447583 | ||
The PDF version of the paper is available on Google Scholar. | ||
|
||
|
||
>>> import numpy as np | ||
>>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: The function only accepts integer values | ||
>>> outcome_of_rolling_n_sided_dice_k_time(-1,5) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Side count should be more than 1 | ||
>>> outcome_of_rolling_n_sided_dice_k_time(3,-2) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Roll count should be more than 0 | ||
|
||
>>> outcome_of_rolling_n_sided_dice_k_time(2,2) | ||
([2, 3, 4], array([0.25, 0.5 , 0.25])) | ||
|
||
>>> outcome_of_rolling_n_sided_dice_k_time(4,2) | ||
([2, 3, 4, 5, 6, 7, 8], array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])) | ||
""" | ||
|
||
if n_side != int(n_side) or k_time != int(k_time): | ||
raise ValueError("The function only accepts integer values") | ||
if n_side < 2: | ||
raise ValueError("Side count should be more than 1") | ||
if k_time < 1: | ||
raise ValueError("Roll count should be more than 0") | ||
if k_time > 100 or n_side > 100: | ||
raise ValueError("Limited to 100 sides or rolling to avoid memory issues") | ||
|
||
probability_distribution = 1 | ||
distribution_step = np.ones(n_side)/n_side | ||
|
||
iter1 = 0 | ||
while iter1 < k_time: | ||
probability_distribution =np.convolve(probability_distribution, distribution_step) | ||
iter1 = iter1+1 | ||
|
||
index = list(range(k_time,k_time*n_side+1)) | ||
return index, probability_distribution | ||
|
||
|
||
''' | ||
# Extra code for the verification | ||
|
||
index_dist = outcome_of_rolling_n_sided_dice_k_time(6, 5) | ||
|
||
print("Indexes:",index_dist[0]) | ||
print("Distribution:",index_dist[1], "Their summation:",np.sum(index_dist[1])) | ||
|
||
import matplotlib.pyplot as plt | ||
plt.bar(index_dist[0], index_dist[1]) | ||
|
||
''' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function:
outcome_of_rolling_n_sided_dice_k_time
. If the function does not return a value, please provide the type hint as:def function() -> None: