Last active
April 24, 2023 14:34
-
-
Save bennett39/c33035da1a5cd68b8569914f53c3fc8c to your computer and use it in GitHub Desktop.
I was curious about the distribution of results for dice. So, I wrote a few lines of Python.
This file contains 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
def dice_distribution(num_sides: int) -> dict[int, float]: | |
""" | |
Get the distribution of possible results of two thrown dice | |
""" | |
distribution = {} | |
for x in dice_generator(num_sides): | |
for y in dice_generator(num_sides): | |
value = x + y | |
distribution.setdefault(value, 0) | |
distribution[value] += 1 | |
num_possibilities = num_sides ** 2 | |
distribution = {k: round((v / num_possibilities), 2) for k, v in distribution.items()} | |
print(distribution) | |
def dice_generator(num_sides: int): | |
for side in range(1, num_sides+1): | |
yield side | |
dice_distribution(6) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment