in reply to Random data generation.
This can be solved simply by viewing the set as a circular list. Just pick one of the next N-1 characters.
# No two repeated characters my @set = qw( a b c d e f ); my $len = 12; my $s = ''; my $i = int(rand(@set)); for (1..$len) { $s .= $set[$i]; $i = ( $i + int(rand($#set)) + 1 ) % @set; } print("$s\n");
dbadfbabfbcd deabfadaeafb edbaceabacad
Update: Fixed bugs, then I realized I misread the question. On the plus side, the solution can easily be adapted to solve the actual problem.
# No more than two repeated characters my @set = qw( a b c d e f ); my $len = 12; my $s = ''; my $i; for (1..$len) { if ($s =~ /(.)\1\z/) { $i = ( $i + int(rand($#set)) + 1 ) % @set; } else { $i = int(rand(@set)); } $s .= $set[$i]; } print("$s\n");
aadbadfafbfb dbfaefababea efeabbcebbee
|
|---|