Advertisement
Guest User

Untitled

a guest
Dec 31st, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import random
  4.  
  5. def main():
  6.     trials = 1000000
  7.     sets = []
  8.     for i in range(trials):
  9.         a = 0   # first (eldest) child is always a girl
  10.         b = random.randrange(2)   # parents have second child (coinflip)
  11.         sets.append((a, b))
  12.  
  13.     num_boys = 0  # number of times the edlest's sibling is a boy
  14.     num_girls = 0 # number of times the edlest's sibling is a girl
  15.     for s in sets:
  16.         if s[1] == 1:  # sibling is a boy
  17.             num_boys += 1
  18.         else:
  19.             num_girls += 1
  20.  
  21.     print ("Number of times sibling is a boy: %d" % num_boys)
  22.     print ("Number of times sibling is a girl: %d" % num_girls)
  23.     print ("Probability of sibling being a boy: %.2f%%" % (num_boys / (num_boys + num_girls) * 100))
  24.  
  25. if __name__ == '__main__':
  26.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement