in reply to Random data generation.
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.#!/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 2: I cleaned up the code a bit though the algorithm stayed the same. I also fixed the bugs as shown below.
Cheers - L~R
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Random data generation.
by BrowserUk (Patriarch) on Jun 27, 2010 at 16:40 UTC | |
by Limbic~Region (Chancellor) on Jun 27, 2010 at 17:02 UTC | |
by BrowserUk (Patriarch) on Jun 27, 2010 at 17:13 UTC |