Last active
May 10, 2022 15:29
-
-
Save craiga/977d2ae5450af2241d5e0f137938f440 to your computer and use it in GitHub Desktop.
Roll a bunch of dice a bunch of times.
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
""" | |
Roll a bunch of dice a bunch of times. | |
""" | |
import random | |
if __name__ == "__main__": | |
results = {i: 0 for i in range(5, 51)} | |
iterations = 0 | |
try: | |
print("Rolling dice over and over. Press Ctrl+C to stop and see results.") | |
while True: | |
d4 = random.randint(1, 4) | |
d6 = random.randint(1, 6) | |
d8 = random.randint(1, 8) | |
d12 = random.randint(1, 12) | |
d20 = random.randint(1, 20) | |
result = sum([d4, d6, d8, d12, d20]) | |
results[result] += 1 | |
iterations += 1 | |
except KeyboardInterrupt: | |
print() | |
for value, result_count in results.items(): | |
bar = "*" * int(result_count / iterations * 2000) | |
print(f"{value}\t{result_count / iterations * 100:.3f}%\t{bar}") | |
print(f"{iterations:,} iterations") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: