If Jeff wrote code that yielded 2/3, then he was implementing my "algorithm 1" (which has selection), not the second algorithm (which does not do any selection).
I just updated my post with an explanation that may clear things up for you.
import random
jeff_stats = { 'boy_and_girl': 0, 'two_girls': 0 }
paul_stats = { 'boy_and_girl': 0, 'other': 0 }
for i in xrange(100000):
children = [random.randint(0, 1), random.randint(0, 1)]
# Jeff's algorithm
if children[0] or children[1]:
print 'I have a girl'
if children[0] and children[1]:
jeff_stats['two_girls'] += 1
else:
jeff_stats['boy_and_girl'] += 1
else:
print "Oops, I'm a liar - I have no girls, so don't count me"
# Paul's algorithm
print 'I have a ' + ('girl' if children[0] else 'boy')
if children[0] != children[1]:
paul_stats['boy_and_girl'] += 1
else:
paul_stats['other'] += 1
print "Jeff stats: " + str(jeff_stats)
print "Paul stats: " + str(paul_stats)
Output (ignoring the actual printouts for each person): Jeff stats: {'boy_and_girl': 50027, 'two_girls': 24949}
Paul stats: {'boy_and_girl': 50027, 'other': 49973}
I liked tromino's explanation best: it's really an ambiguity in the English language. Under your 2nd algorithm, 1/4 of parents can't truthfully announce "I have a girl" - but in ordinary conversation, this doesn't matter, because they'd just truthfully say "I have a boy" and we don't make a distinction. It's only when we explicitly filter out parents who don't have a boy that we change the probabilities.It can still be the case that the chances of there being a boy and a girl are 1/2, while the chances of there being a boy after you find out about a girl are 1/3, purely because you don't know whether you found out about an older or younger girl. Probability is just a function of our ignorance (at least in this case; I'm not qualified to have an opinion about whether it always is).
[Edit: Hm. Looking again, it seems Jeff Atwood's code is for a different problem he was also discussing, so nevermind. After considering your update, I think you might be right.]
[Further edit follows:]
Here's some python that essentially shows you're correct, unless I flubbed it:
firstpickwasboy = {'boy':0, 'girl':0}
firstpickwasgirl = {'boy':0, 'girl':0}
choices = ['boy', 'girl']
for i in range(100000):
children = [random.choice(choices), random.choice(choices)]
whichchild = random.randint(0, 1)
otherchild = 1
if(whichchild): otherchild = 0
if(children[whichchild]=='boy'):
firstpickwasboy[children[otherchild]] += 1
else:
firstpickwasgirl[children[otherchild]] += 1
>>> firstpickwasboy
{'boy': 25132, 'girl': 25116}
>>> firstpickwasgirl
{'boy': 24679, 'girl': 25073}
Nor is that what I said. :)
Assuming you meant "chance of being a boy" where you said "chance of being a girl", I agree that announcing the gender of one of the two children increases the probability of the other child being the opposite gender from 1/2 to 2/3.
The reason this is so is that some of the original probability was sunk in a case you've now eliminated: the case where there were two of the gender you didn't announce.
[Edit: Jeff already wrote some code, and while I haven't bothered to review it, I assume the lack of outcry about it indicates its correctness. Care to share where he got that wrong?]