#!/usr/bin/perl

#
# Royal Family Simulation
# A royal family has children until it has a boy or until it has three
# children, whichever comes first. Assume that each child is a girl (or
# boy) with probability 1/2. Find the expected number of boys and the
# expected number of girls in this royal family. 
#
# This simulation only simulates one family and prints the result
#
  
$c = 0;			# Number of children at the moment
$b = 0; $g = 0;		# Boys and girls in the family
until($c == 3) {
  if(rand(1) < 0.5) {	# 50-50 chance
    $b++;		# Having a boy
    $c = 3;		# Set t to be 3 so no more children are born
  } else {
    $g++;		# Having a girl
    $c++;		# Add one to c, the number of children
  }
}

print "This royal family has $b boy(s) and $g girl(s)\n";
