Skip to content

Instantly share code, notes, and snippets.

@macterra
Created December 28, 2010 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macterra/757093 to your computer and use it in GitHub Desktop.
Save macterra/757093 to your computer and use it in GitHub Desktop.
import random
nCouples = 1000000
nMales = nCouples/2
nFemales = nCouples/2
nSonlessCouples = nCouples
while nSonlessCouples > 0:
for i in range(nSonlessCouples):
if random.random() < 0.5:
# it's a boy! stop trying
nMales = nMales + 1
nSonlessCouples = nSonlessCouples - 1
else:
# it's a girl! try again
nFemales = nFemales + 1
print 1. * nFemales / (nMales + nFemales)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment