#!/usr/bin/perl # Tom Blum # May 2007 # IWA-HWG Homework/ Instructor M. Kowalski print "\nThis program counts the number of simulated rolls \n"; print "of two dice in order to get \"snake eyes\" (two ones)\n"; print "or \"box cars\" (two sixes).\n\n"; print "It performs this simulation over a number of trials and\n"; print "determines the associated average and standard deviation.\n"; print "\nEnter the number of trials you would like to run: "; chomp($sample_size = ); if($sample_size < 1) { print "\nPlease enter the number of trials (e.g. 1000): "; chomp($sample_size = ); if($sample_size < 1) { $sample_size = 1000; print "We will use a sample size of $sample_size.\n"; } } $sum_roll_count = 0; $sum_roll_count_squared = 0; for($i=0; $i<$sample_size; $i++) { $die1=0; $die2=0; $roll_count = 0; until($die1+$die2==2 || $die1+$die2==12) { $roll_count++; $die1 = int(1 + rand 6); $die2 = int(1 + rand 6); #print "$roll_count: \t $die1,\t $die2\n"; } #print "It required $roll_count rolls to roll snake eyes.\n"; $sum_roll_count += $roll_count; $sum_roll_count_squared += $roll_count**2; } $roll_count_average = $sum_roll_count / $sample_size; $roll_count_squared_average = $sum_roll_count_squared / $sample_size; $standard_deviation = sqrt($roll_count_squared_average-$roll_count_average**2); print "\nUsing a sample size of $sample_size, the following results were obtained.\n"; print "The average number of rolls to roll snake eyes or box cars is $roll_count_average.\n"; print "The standard deviation is $standard_deviation.\n";