-
代码一
-
代码二
-
参考资料
$k = 輸出數量; @lines; $n = 0; srand; while(《》) { $n++; if ($n 《= $k) { push(@lines, $_); } else { $r = int(rand($n)); if ($r 《 $k) { $lines[$r] = $_; } } } |
#!/usr/bin/perl die "Usage: $0 , where N is the number of lines to pickn" 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; |