my @buffer; push @buffer, scalar for 1 .. $N; while (my $line = ) { next unless rand($.) < $N; $buffer [rand @buffer] = $_; } print @buffer;