fork download
  1. import random
  2.  
  3. GENDERS = 'boy girl'.split()
  4. DAYS = 'mon tue wed thu fri sat sun'.split()
  5.  
  6. def birth(): # The miracle of life!
  7. return random.choice(GENDERS), random.choice(DAYS)
  8.  
  9. def special(c):
  10. return c[0] == 'boy' and c[1] == 'tue'
  11.  
  12. boy_pair = 0
  13. misc_pair =0
  14. for _ in xrange(1000000):
  15. c1, c2 = birth(), birth() # A guy gets two kids
  16. if special(c1) or special(c2): # Hey, random dude, is any of your children special?
  17. if c1[0] == 'boy' and c2[0] == 'boy':
  18. boy_pair += 1
  19. else:
  20. misc_pair += 1
  21.  
  22. print boy_pair, misc_pair, boy_pair + misc_pair, float(boy_pair) / (boy_pair + misc_pair)
  23. print 13.0/27
Success #stdin #stdout 4.92s 10024KB
stdin
Standard input is empty
stdout
66105 71362 137467 0.480879047335
0.481481481481