-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Add Monte Carlo dice simulation algorithm #1759
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4254e30
Implement basic dice simulation
naviji e2d145b
Add tests to throw_dice
naviji c001165
Fix comment
naviji f36edcf
Add type hints
naviji 0cd6c49
Add additional comments
naviji 86afd8c
Update monte_carlo_dice.py
cclauss 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
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,45 @@ | ||
import random | ||
from typing import List | ||
|
||
class Dice: | ||
NUM_SIDES = 6 | ||
|
||
def __init__(self): | ||
""" Initialize a six sided dice """ | ||
self.sides = list(range(1, Dice.NUM_SIDES + 1)) | ||
|
||
def roll(self): | ||
return random.choice(self.sides) | ||
|
||
def _str_(self): | ||
return "Fair Dice" | ||
|
||
|
||
def throw_dice(num_throws: int, num_dice: int=2) -> List[float]: | ||
""" | ||
Return probability list of all possible sums when throwing dice. | ||
|
||
>>> random.seed(0) | ||
>>> throw_dice(10, 1) | ||
[10.0, 0.0, 30.0, 50.0, 10.0, 0.0] | ||
>>> throw_dice(100, 1) | ||
[19.0, 17.0, 17.0, 11.0, 23.0, 13.0] | ||
>>> throw_dice(1000, 1) | ||
[18.8, 15.5, 16.3, 17.6, 14.2, 17.6] | ||
>>> throw_dice(10000, 1) | ||
[16.35, 16.89, 16.93, 16.6, 16.52, 16.71] | ||
>>> throw_dice(10000, 2) | ||
[2.74, 5.6, 7.99, 11.26, 13.92, 16.7, 14.44, 10.63, 8.05, 5.92, 2.75] | ||
""" | ||
dices = [Dice() for i in range(num_dice)] | ||
count_of_sum = [0] * (len(dices) * Dice.NUM_SIDES + 1) | ||
for i in range(num_throws): | ||
count_of_sum[sum([dice.roll() for dice in dices])] += 1 | ||
probability = [round((count * 100) / num_throws, 2) for count in count_of_sum] | ||
return probability[num_dice:] # remove probability of sums that never appear | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
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.
Uh oh!
There was an error while loading. Please reload this page.