use strict; my $size = shift @ARGV || 3; # sample size my @sample; # read n lines for (1..$size) { push @sample, scalar <>; } # shuffle - I'll do it in pure perl. my $i = $size; while ($i--) { my $j = int rand($i + 1); @sample[$i, $j] = @sample[$j, $i]; } # now read the rest of the lines in. while (<>) { my $choice = int rand($.); if ($choice < $size) { $sample[$choice] = $_; } } # and minimize work by chomping as few lines as possible. chomp(@sample);