Skip to content

Instantly share code, notes, and snippets.

@Razatastic
Last active May 4, 2020 07:13
Show Gist options
  • Save Razatastic/54bea6ef135ac3946033f2062d0328c4 to your computer and use it in GitHub Desktop.
Save Razatastic/54bea6ef135ac3946033f2062d0328c4 to your computer and use it in GitHub Desktop.
Simulate dice rolls and see probability between 2 to 12.
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