Skip to content

Instantly share code, notes, and snippets.

@DavidAntliff
Created October 22, 2018 11:28
Show Gist options
  • Save DavidAntliff/2a4655667b3ed3fd11b0e9d7cbf2f296 to your computer and use it in GitHub Desktop.
Save DavidAntliff/2a4655667b3ed3fd11b0e9d7cbf2f296 to your computer and use it in GitHub Desktop.
Simple simulation of rolling dice
#!/usr/bin/env python
# Python 3.x
import random
rolls = 5000 # the more results the better
scaling = 10 # scale the histogram to fit the screen
dice = (20,) # a single d20
#dice = (6, 6) # two d6s
results = dict()
for roll in range(rolls):
sum = 0
for die in dice:
sum += random.randint(1, die)
results[sum] = results.get(sum, 0) + 1
for result_key in sorted(results):
print("{value:-2} : {bar}".format(value=result_key,
bar="#" * int(results[result_key] / scaling)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment