This is code testing the two child problem to see if it really works. The result is 33%, so apparently it does. See http://sciencenews.org/view/generic/id/60598/title/When_intuition_and_math_probably_look_wrong for the original article I am checking out.

package test;

import java.util.Random;

public class Test2CProb
{
	public static void main(String[] args)  {
		int numSecondBoys = 0;
		int numSecondGirls = 0;
		
		Random rand = new Random(System.currentTimeMillis());
		for(int ix=0; ix<100000; ++ix) {
			// Generate a 2 child family.
			boolean isBoy1 = rand.nextBoolean();
			boolean isBoy2 = rand.nextBoolean();
			
			// Ignore the situation where both are girls.
			if(isBoy1 || isBoy2) {
				// Choose a random child to be "the boy".
				boolean useFirst = rand.nextBoolean();

				// If I use continues here to ignore accidentally pointing at a girl then I
				// get a 50% chance. If I simply point toward the one that is a boy then
				// I get a 33% chance.
				if(useFirst && !isBoy1)
					useFirst = false;
//					continue;
				else if(!useFirst && !isBoy2)
					useFirst = true;
//					continue;
				
				if(useFirst) {
					if(isBoy2)
						++numSecondBoys;
					else
						++numSecondGirls;
				} else {
					if(isBoy1)
						++numSecondBoys;
					else
						++numSecondGirls;
				}
				
			}
		}
		
		int total2ndChilds = numSecondBoys + numSecondGirls;
		double boy2ndChance = (double)numSecondBoys / (double)total2ndChilds;
		System.out.println("Total="+ total2ndChilds +" Chance="+ boy2ndChance );
	}

}
Version 1.1 last modified by Geoff Fortytwo on 29/06/2010 at 11:50

Attachments 0

No attachments for this document
Website Top
Send Me Mail!:
   g42website4 AT g42.org
My Encyclopaedia Blog

Creator: Geoff Fortytwo on 2010/06/29 11:47
Copyright 2004-2007 (c) XPertNet and Contributing Authors
1.3.2.9174