Guest User

Untitled

a guest
May 23rd, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. import random                                                                        
  2.  
  3. def coinflip():
  4.     return random.choice((True, False))
  5.  
  6.  
  7. kids = {'Girl': 0, 'Boy': 0}
  8. brothers = {'Girl': 0, 'Boy': 0}
  9.  
  10. for _ in range(1000000):
  11.     boy_count = 0
  12.     family = []
  13.  
  14.     for _ in iter(coinflip, False):
  15.         kid = random.choice(('Girl', 'Boy'))
  16.         family.append(kid)
  17.         if kid == 'Boy':
  18.             boy_count += 1
  19.  
  20.     for kid in family:
  21.         kids[kid] += 1
  22.         brothers[kid] += boy_count - int(kid == 'Boy')
  23.  
  24.  
  25. print('Out of a sample of', kids['Girl'], 'girls, and', kids['Boy'], 'boys:')
  26. print('The average girl has', brothers['Girl']/kids['Girl'], 'brothers.')
  27. print('The average boy has', brothers['Boy']/kids['Boy'], 'brothers.')
  28. print('Ratio:', (brothers['Girl']/kids['Girl'])/(brothers['Boy']/kids['Boy']))
Advertisement
Add Comment
Please, Sign In to add comment