Monday, May 4, 2009

Selecting N random lines from a file

#!/usr/bin/perl -w

die "Usage: $0 [N] [FileName], where N is the number of lines to pick\n"
if @ARGV < 1;
$N = shift @ARGV;

@pick = ();
while (<>) {
if (@pick < $N) {
push @pick, $_;
($r1, $r2) = (rand(@pick), rand(@pick));
($pick[$r1], $pick[$r2]) = ($pick[$r2], $pick[$r1]);
} else {
rand($.) <= $N and $pick[rand(@pick)] = $_;
}
}

print @pick;

No comments: