Perl in 21 Days

by David Till

Random Sampling

Assuming that two populations of 40 subjects are identical for some measurable aspect of their physiology (for instance, systolic blood pressure), what are the chances that a random sample of five subjects from each population will reflect this? How reliable is random sampling?

Random Sampling Script

Consider the following Perl script:

#!/usr/bin/perl -w use strict; my @pop = (62, 65, 66, 72, 80, 75, 67, 73, 79, 69, 70, 64, 64, 65, 67, 81, 61, 82, 68, 59, 83, 77, 73, 74, 66, 67, 77, 78, 82, 80, 73, 68, 67, 73, 84, 58, 75, 76, 72, 68); my $actual_average = 0; my $num = @pop; my $tally = 0; foreach my $n (@pop){ $tally+=$n; } $actual_average = $tally/$num; print "ACTUAL AVERAGE: $actual_average\n"; srand(time); my $s1 = 0; my @g1 = (); my $i = 0; #index $tally = 0; foreach my $r (0..4){ $i = int rand($num); $tally += $pop[$i]; push(@g1,$pop[$i]); } $s1 = $tally/5; print "SAMPLE: @g1, SAMPLE AVERAGE: $s1\n"; my $s2 = 0; my @g2 = (); $tally = 0; foreach my $r (0..4){ $i = int rand($num); $tally += $pop[$i]; push(@g2,$pop[$i]); } $s2 = $tally/5; print "SAMPLE: @g2, SAMPLE AVERAGE: $s2\n"; exit; Run this sample script several times. Make sure you pause for a couple seconds between each run of the script since srand needs a new value to produce unique output.

Notice the use of push to add an item to an array. This is a very useful trick in a lot of circumstances. One particularly useful context for this trick is when you are creating tables in a CGI script.

  • Run the Perl script shown above ten times. Redirect the output to a file so that it can be easily reformatted into an HTML page. To redirect you do this: ./perlScript.pl >> output_file.txt