Guest User

Untitled

a guest
Jul 8th, 2021
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. import random
  2.  
  3. total_births = []
  4.  
  5. def give_birth():
  6.     return random.choice(['G', 'B'])
  7.  
  8. def give_birth_until_boy():
  9.     baby = give_birth()
  10.     family_births = [baby]
  11.     while (baby == 'G'):
  12.         baby = give_birth()
  13.         family_births.append(baby)
  14.     return family_births
  15.  
  16. def printCount(n, births):
  17.     boyCount, girlCount = births.count('B'), births.count('G')
  18.     print(f"n = {n}\t\tboys = {boyCount}\t\tgirls {girlCount}")
  19.  
  20. def simulate_families(n):
  21.     for i in range(n):
  22.         family_babies = give_birth_until_boy()
  23.         total_births.extend(family_babies)
  24.         printCount(n, family_babies)
  25.     printCount('all', total_births)
  26.  
  27. simulate_families(100000)
Advertisement
Add Comment
Please, Sign In to add comment