Implementation of the Landsburg challenge


/ Published in: Python
Save to your folder(s)

In response to this: http://www.thebigquestions.com/2010/12/27/win-landsburgs-money/


Copy this code and paste it in your HTML
  1. #!/usr/bin/python
  2.  
  3. import random
  4.  
  5. def run_sim(num_families, num_years):
  6. families = {}
  7. for family in range(num_families):
  8. families[family] = []
  9.  
  10. for year in range(num_years):
  11. for family, children in families.items():
  12. if len(children) == 0 or children[-1] == 'G':
  13. next_child = random.choice(['B', 'G'])
  14. children.append(next_child)
  15.  
  16. total_girls = 0
  17. total_boys = 0
  18. for family, children in families.items():
  19. for child in children:
  20. if child == 'B':
  21. total_boys += 1
  22. else:
  23. total_girls += 1
  24. ratio = total_girls/float(total_girls + total_boys)
  25. return ratio
  26.  
  27.  
  28. ratios = []
  29. for i in range(3000):
  30. ratios.append(run_sim(10, 30))
  31.  
  32. print sum(ratios)/len(ratios)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.