use strict; my $size = shift @ARGV || 3; # sample size my @sample; my $taken = 0; # for making sure we get the first $size lines while( my $line = <> ) { chomp $line; if ( rand(1) < ($size/$.) ){ my $position; do{ $position = int rand($size); } while( $taken < $size && $sample[$position] ); $sample[$position] = $line; $taken++; } } for( my $i = 0; $i < @sample; $i++ ){ print "[$i] '$sample[$i]'\n"; } print "\n";