Skip to content

Instantly share code, notes, and snippets.

@kazuho
Created November 17, 2021 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazuho/bfb6bf9dbf597133cbb3e2da931a4084 to your computer and use it in GitHub Desktop.
Save kazuho/bfb6bf9dbf597133cbb3e2da931a4084 to your computer and use it in GitHub Desktop.
#! perl
use strict;
use warnings;
my $lines_to_pick = 5; # 何行選ぶか
my $lines_total = 0;
my @picked;
while (my $line = <STDIN>) {
++$lines_total;
if (@picked < $lines_to_pick) {
push @picked, $line;
} elsif (rand() < $lines_to_pick / $lines_total) {
$picked[int(rand() * $lines_to_pick)] = $line;
}
}
print $_ for @picked;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment