从文本文件中随机提取数行的代码

  • 代码一

  • $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;
  • 参考资料

  • 水塘抽样(维基百科)
    pick N random lines from a file