Skip to content

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 6 commits into from
Feb 17, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update monte_carlo_dice.py
  • Loading branch information
cclauss authored Feb 17, 2020
commit 86afd8c197d591007f3bc3e98ccf271856dd8d3b
3 changes: 0 additions & 3 deletions maths/monte_carlo_dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ def throw_dice(num_throws: int, num_dice: int=2) -> List[float]:
>>> 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

Expand Down