It looked reasonably random to me. I ran it through the same program I ran my snippet through and got similar results. Maybe slightly less uniform than mine, but still reasonable.
Here is the code I used to test these. I create a list of 100 items, and randomize that list and record the resulting positions 100,000 times. The average positions for each number should hover around the mid-point of the list. Both of these do. Here's the code( yes, it's a quick hack!)
#!/usr/bin/perl
print `date`;
my %count;
my $size = 100000;
foreach (0..99) {
$count{$_} = 0;
};
my @lKeys = sort { $a <=> $b } (keys(%count));
my $arraySize = scalar(@lKeys);
for (1..$size) {
my @lList = crypto(\@lKeys);
my $pos = 0;
my $curr = '';
while (defined($curr = shift(@lList))) {
$count{$curr} += $pos;
$pos++;
}
}
local $\ = "\n";
print "Size:$size";
print "Array Size: $arraySize\n";
foreach my $curr (@lKeys) {
my $total = $count{$curr};
my $average = $total/$size;
my $diff = abs(($arraySize/2)-$average);
print "$curr:\t$diff";
}
print `date`;
exit;
sub crypto {
my $rList = shift;
my @list = @$rList;
my ($lPos, $lRand);
foreach $lPos (1..$#list)
{
$lRand = int(rand($lPos+1));
@list[$lPos, $lRand] = @list[$lRand, $lPos];
}
return @list;
}
sub otherCrypto {
my $rList = shift;
my @list = sort {rand > .5} @$rList;
return @list;
}
If there is a problem with my methodology for testing this, I would appreciate it being pointed out.
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.