Skip to content

Instantly share code, notes, and snippets.

@philihp
Last active August 29, 2015 14:01
Show Gist options
  • Save philihp/276c356ed2bc2d4e7020 to your computer and use it in GitHub Desktop.
Save philihp/276c356ed2bc2d4e7020 to your computer and use it in GitHub Desktop.
Monte carlo simulator finding the odds that an 8-digit decimal number has exactly 4 of the same digit.
#!/usr/bin/python
import random
total = 0;
good = 0;
for n in xrange(1,100000000):
i = random.randint(0,99999999)
s = "{0:0>8}".format(i);
a = {}
for c in list(s):
if c in a.keys():
a[c]+=1;
else:
a[c]=1;
if 4 in a.values():
good+=1;
if n % 100000 == 0:
ratio = good/float(n)
print "%.4f = %d / %d"%(ratio,good,n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment