Last active
May 4, 2020 07:13
-
-
Save Razatastic/54bea6ef135ac3946033f2062d0328c4 to your computer and use it in GitHub Desktop.
Simulate dice rolls and see probability between 2 to 12.
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
| from random import randint | |
| # Simulate 2 dice being rolled | |
| def dieRoll(): | |
| return randint(1, 6) + randint(1, 6) | |
| # Store results in a dictionary | |
| results = {n: 0 for n in range(2, 13)} | |
| # Ask user how many tests they want to run | |
| rolls = int(input("How many die rolls do you want to simulate? ")) | |
| # Roll the dice 1,000,000 times | |
| for x in range(0, rolls): | |
| results[dieRoll()] += 1 | |
| # Calculate percentages | |
| for key, value in results.items(): | |
| print(key, ':', "{:.1%}".format(value/rolls)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment