BrowserUk,
I haven't read any of the other solutions but this is the first idea that popped into my head. I haven't really tested it nor is it without at least 1 assumption.
#!/usr/bin/perl
use strict;
use warnings;
my @M = 'a' .. 'z';
my $N = 42; # Assumes N is > 2
my ($minus_1, $minus_2) = ($M[rand @M], $M[rand @M]);
my $str = $minus_1 . $minus_2;
for (1 .. $N - 2) {
my $idx = rand @M;
$idx = ($idx + (int(rand $#M) + 1)) % @M if $minus_1 eq $minus_2 &
+& $M[$idx] eq $minus_2;
$str .= $M[$idx];
($minus_1, $minus_2) = ($minus_2, $M[$idx]);
}
print "$str\n";
Update: Now after reading the solutions, I realize mine is almost identical to
ikegami's. I chose to use scalars to hold the value of the last two chars instead of the regex, but very similar indeed.
Update 2: I cleaned up the code a bit though the algorithm stayed the same. I also fixed the bugs as shown below.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.