good chemistry is complicated,and a little bit messy -LW PerlMonks

comment on

 Need Help??

FWIW. To the best of my ability to test it, and walking on the shoulders of those better versed in statistics than I, my implementation of Fisher-Yates algorithm compares favourably with that given in the FAQ.

Perhaps you would point out the flaws in my testing method?

Test code

```#!/usr/bin/perl -w
use strict;
use vars qw/\$size \$iter/;
use Benchmark qw(cmpthese);
use Data::Dumper;

\$size ||= 1000;
\$iter ||= 1000;

sub FAQ_FY {
my \$array = shift;
my \$i;
for (\$i = @\$array; --\$i; ) {
my \$j = int rand (\$i+1);
next if \$i == \$j;
@\$array[\$i,\$j] = @\$array[\$j,\$i];
}
}

sub shuffl (\@) {
my \$r=pop;
\$a = \$_ + rand @{\$r} - \$_ and @\$r[\$_, \$a] = @\$r[\$a, \$_]
for (0..\$#{\$r});
}

my @array = 1 .. \$size;

cmpthese(-1, {
FAQ_FY    => sub { FAQ_FY \@array },
shuffl    => sub { shuffl  @array },
});

my (%buckets, %d, @temp);;
my @set = qw(A B C D);

for (1 .. \$iter ) {
@temp=@set; FAQ_FY \@temp; \$buckets{"@temp"}{FAQ_FY}++;
@temp=@set; shuffl  @temp; \$buckets{"@temp"}{shuffl}++;
}

print "\npermutation |  FAQ_FY |  shuffl \n";
print "----------------------------------\n";
for my \$key (sort keys %buckets) {
printf "%8.8s:   |   %4d |   %4d \n", \$key,
\$buckets{\$key}{FAQ_FY},
\$buckets{\$key}{shuffl},

\$d{FAQ_FY}{Ex} += \$buckets{\$key}{FAQ_FY}; \$d{FAQ_FY}{Ex2} += \$buck
+ets{\$key}{FAQ_FY}**2;
\$d{shuffl}{Ex} += \$buckets{\$key}{shuffl}; \$d{shuffl}{Ex2} += \$buck
+ets{\$key}{shuffl}**2;
}
print "------------------------------------------------------\n";
printf "Std. Dev.   |  %0.3f |  %0.3f \n",
sqrt( (\$d{FAQ_FY}{Ex2} - (\$d{FAQ_FY}{Ex}**2/24))/23 ),
sqrt( (\$d{shuffl}{Ex2} - (\$d{shuffl}{Ex}**2/24))/23 );

__END__

```C:\test>199981 -size=100 -iter=100000
Benchmark: running FAQ_FY, shuffl, each for at least 1 CPU seconds...
FAQ_FY:  2 wallclock secs ( 1.14 usr +  0.00 sys =  1.14 CPU) @ 39
+2.64/s (n=448)
shuffl:  1 wallclock secs ( 1.08 usr +  0.00 sys =  1.08 CPU) @ 44
+2.70/s (n=479)
Rate FAQ_FY shuffl
FAQ_FY 393/s     --   -11%
shuffl 443/s    13%     --

permutation |  FAQ_FY |  shuffl
----------------------------------
A B C D:   |   4140 |   4221
A B D C:   |   4231 |   4211
A C B D:   |   4205 |   4151
A C D B:   |   4247 |   4257
A D B C:   |   4076 |   4275
A D C B:   |   4107 |   4244
B A C D:   |   4207 |   4165
B A D C:   |   4159 |   4240
B C A D:   |   3941 |   4187
B C D A:   |   4136 |   4116
B D A C:   |   4158 |   4126
B D C A:   |   4185 |   4111
C A B D:   |   4126 |   4231
C A D B:   |   4224 |   4122
C B A D:   |   4135 |   4139
C B D A:   |   4123 |   4097
C D A B:   |   4179 |   4001
C D B A:   |   4228 |   4175
D A B C:   |   4295 |   4057
D A C B:   |   4264 |   4131
D B A C:   |   4206 |   4169
D B C A:   |   4138 |   4239
D C A B:   |   4167 |   4208
D C B A:   |   4123 |   4127
------------------------------------------------------
Std. Dev.   |  72.382 |  67.643

C:\test>

Nah! You're thinking of Simon Templar, originally played (on UKTV) by Roger Moore and later by Ian Ogilvy
Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• 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.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, 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?

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2020-12-03 20:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How often do you use taint mode?

Results (57 votes). Check out past polls.

Notices?