Skip to content

Instantly share code, notes, and snippets.

@kitroed
Last active August 29, 2015 14:01
Show Gist options
  • Save kitroed/d80be12c20c7a55eb0c4 to your computer and use it in GitHub Desktop.
Save kitroed/d80be12c20c7a55eb0c4 to your computer and use it in GitHub Desktop.
r/dailyprogrammer challenge number 163 "easy" - Probability Distribution of a 6 Sided Di
# r/dailyprogrammer challenge number 163 "easy"
# Probability Distribution of a 6 Sided Di
# Author: Kit Roed
# Language: python3
#make python random do all the work
import random
dsize = 6
roll_counts = []
# init list to store roll counts
for s in range(0, dsize):
roll_counts.append(0)
# print the top of the table ("end" overrides the default \n)
print('# of Rolls ', end=' ')
for i in range(1, dsize + 1):
print(('%ds' % i).ljust(6), end=' ')
# print the '=' row (11 for first column, then 7 times the number of sides)
print('\n' + '='*(11+(dsize*7)))
# a MILLION (plus one to get the last run to go)
for x in range(1, 1000001):
roll_counts[random.randrange(dsize)] += 1
if (x in (10, 100, 1000, 10000, 100000, 1000000)):
print('{0:<11d}'.format(x), end=' ')
for r in range(0, dsize):
print('{0:>5.2f}%'.format(roll_counts[r]/x*100), end=' ')
# one more print to end the line
print()
# Output:
# # of Rolls 1s 2s 3s 4s 5s 6s
# =====================================================
# 10 30.00% 10.00% 0.00% 30.00% 20.00% 10.00%
# 100 18.00% 11.00% 13.00% 26.00% 22.00% 10.00%
# 1000 15.60% 14.70% 17.30% 16.20% 18.40% 17.80%
# 10000 15.65% 16.25% 16.69% 16.71% 17.62% 17.08%
# 100000 16.48% 16.45% 16.72% 16.72% 16.74% 16.88%
# 1000000 16.63% 16.63% 16.70% 16.71% 16.64% 16.69%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment