$| = 1; $seq = ""; $mx = 9; sub other { for (my $w=0; $w<10; $w++) { my $s = substr($seq, -$w, +$w); foreach my $v (0..$w) { if (index($seq, $s . $v)<0) { $seq .= $v; return $v; } } } die "d'oh"; } foreach my $n (1..10_000) { my $v = other(); print "$n $v\n"; }