my $x = rand(65536); foreach (@lines) { open(my $fh_out, '>', sprintf('prefix%04X.txt', $x)) or die; print $fh_out $_; $x = ($x + 1) % 65536; }