#!/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 three : histogram of number of white balls
#

# Set up variables
$w = 1;			# Number of white ($w) marbles
$t = 1000;		# Number of trials in each game
$f = "polyaurn.txt";	# File name to print results to
$s = ", ";		# What to print after a number
			# Common values:
			#   "\n" to put a return after a number
			#   ", " to put a comma (for Maple list)

print "Enter number of trials to simulate : ";
$g = <STDIN>; chomp($g);
open(FILE,">>$f");

for($j = 0; $j < $g; $j++) {
  $w = 1;		# Reset white marbles
  for($i = 2; $i < $t; $i++) {
    $w += (rand($i) < $w);
  }
  print FILE ("$w$s");
}

close(FILE);
