#!/usr/bin/perl

#
# The Polya urn model
# We start with an urn which contains one white ball and one black ball.
# Every minute we choose a ball at random from the urn, then replace this
# ball and add one more of the same color. 
#
# Version two: play game many times and average the results
#

# Set up variables
$w = 1;			# Number of white ($w) and black ($b) balls
$t = 1000;		# Number of total marbles to have in urn
$tw = 0; $tb = 0;	# Total number of whites and blacks drawn
$g = 1000;		# Number of separate games

for($i = 0; $i < $g; $i++) {
  $w = 1;		# Reset urn
  for($j = 2; $j < $t; $j++) {
    $w += (rand($j) < $w);
  }
  $tw += $w;		# Add to total whites drawn
  $tb += ($t - $w);	# Add to total blacks drawn
}

print "Number of white balls in $g games: $tw\n";
print "Number of black balls in $g games: $tb\n";

