my(@lines, $line); open(FILE, shift) || die; until( scalar @lines == $ARGV[0] ){ seek(FILE, 0, $. = 0); rand($.) < 1 && ($line = $_) while ; push(@lines, $line); } print @lines, "wc -l could have told you this is $. words\n";