in reply to Re^2: Random data generation.
in thread Random data generation.
I wonder if you've overlooked my solution, or if there's anything wrong with it.
Not only is it simpler (both less lines of code and conceptually easier to understand, IMHO), it's also consistently faster than what you found to be the fastest:
$ ./846784.pl Rate salva almut salva 881/s -- -15% almut 1037/s 18% --
(tested with v5.10.1, x86_64-linux-thread-multi)
#!/usr/bin/perl use Benchmark "cmpthese"; my $len = shift @ARGV || 12; my $max_reps = shift @ARGV || 2; sub almut { my @set = qw( A B C D E F ); for (1..100) { my $result = ''; while (length($result) < $len) { my $char = $set[ rand @set ]; next if substr($result, -$max_reps) eq $char x $max_reps; $result .= $char; } } } sub salva { my $set = [ qw( A B C D E F ) ]; for (1..100) { my $out = ''; my $last_ix = -1; my $reps = 0; for (1..$len) { my $ix; if ($reps >= $max_reps) { $ix = int rand(@$set - 1); $ix++ if $ix >= $last_ix; } else { $ix = int rand(@$set); } if ($ix == $last_ix) { $reps++; } else { $last_ix = $ix; $reps = 1; } $out .= $set->[$ix]; } } } cmpthese(-1, { almut => \&almut, salva => \&salva, });
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Random data generation.
by BrowserUk (Patriarch) on Jun 27, 2010 at 18:59 UTC | |
by salva (Canon) on Jun 28, 2010 at 10:16 UTC | |
by BrowserUk (Patriarch) on Jun 28, 2010 at 11:48 UTC | |
by salva (Canon) on Jun 28, 2010 at 13:23 UTC | |
by BrowserUk (Patriarch) on Jun 28, 2010 at 13:41 UTC | |
by furry_marmot (Pilgrim) on Jun 29, 2010 at 17:48 UTC | |
by salva (Canon) on Jun 29, 2010 at 18:24 UTC | |
by furry_marmot (Pilgrim) on Jun 29, 2010 at 18:44 UTC |