#!/usr/bin/perl -w use strict; use warnings; srand(); my $maxday = 365; my $TRIALS = 1024000; my $average = 0; my $sum = 0; my $tenpercent = $TRIALS / 10; for (my $count = 0; $count < $TRIALS; $count++) { $sum += &test_bdays($maxday); if (($count) and (!($count % $tenpercent))) { $average = ($sum * 1.0) / $count; print("After $count trials, ", "the average number of people in the room was $average\n"); } } $average = ($sum * 1.0) / $TRIALS; print("After $TRIALS trials, ", " the average number of people in the room was $average\n"); sub test_bdays { my ($mday) = @_; my @days = (); my $d = $mday; my $people = 0; while ($d) { $days[$d] = 0; $d--; } my $flag = 0; while (!($flag)) { $people++; my $index = int(rand() * $mday); $days[$index]++; $flag = ($days[$index] > 1); } return($people); }