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 = {"0":0,"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0}
for c in list(s):
a[c]+=1;
if 4 in a.values():
good+=1;
if n % 100000 == 0:
ratio = good/float(n)
print "%%%.8f = %d / %d"%(ratio*100,good,n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment