#!/usr/bin/perl -w # QUESTION: A bus runs every 10 minutes. You arrive at the bus stop at # a random time every day. After 1000 days hat is your average waiting time? # SIMULATION: We will simulate this by computing the average of 1000 #random numbers. # While obviously the answer will be about 5 minutes, the point of this # is to see a simple similation in action. $N = 1000; # number of days in our experiment. $TotalTime = 0; # initialize Total Waiting Time. for ($k=1; $k<=$N; $k++) { $TotalTime = $TotalTime + rand(10); } $AverageWaitTime = $TotalTime/$N; print "\n\nAfter $N days, your average waiting time was $AverageWaitTime minutes\n\n"; print "Printing so many decimals seems silly. We keep only 3 decimals.\n"; $Avg3 = sprintf("%.3f", $AverageWaitTime ); print "\tAfter $N days, your average waiting time was $Avg3 minutes\n\n";