Another go, seeing that my previous attempt didn't do what you wanted. I don't see anyone else trying this algorithm, but it may have been done by someone who can write better Perl than I can.
use strict;
use warnings;
my $nRepeats = 2;
my @sSet = qw (a b c d e f);
my $nLength = 12;
my %sSets;
for my $nKey (0..((@sSet) ** $nRepeats - 1)) {
my $sKey = "";
for (1..$nRepeats) {
$sKey .= $sSet[($nKey / (($#sSet + 1) ** ($_ - 1))) % ($#sSet
++ 1)];
}
$sSets{$sKey} = [@sSet];
}
for (0..$#sSet) {
splice(@{$sSets{$sSet[$_] x $nRepeats}}, $_, 1);
}
my $sString;
my @sChr;
for (1..$nRepeats) {
my $sRnd = $sSet[int(rand(@sSet))];
$sString .= $sRnd;
push(@sChr, $sRnd);
}
for ($nRepeats + 1 .. $nLength) {
my $sKey;
for (@sChr) {
$sKey .= $_;
}
my $sRnd = @{$sSets{$sKey}}[int(rand(@{$sSets{$sKey}}))];
$sString .= $sRnd;
push(@sChr, $sRnd);
shift(@sChr);
}
print "$sString \n";
The algorithm was developed on one of the hot nights when you couldn't sleep, either. The Perl has taken me 2 days to write with all the other distractions life affords. I learned some useful stuff writing it (thanks, Tye, for helping me in CB), so I'm putting it up even though you have your solution!
Regards,
John Davies
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|