in reply to RE: RE: Randomize List of items
in thread Randomize List of items
If there is a problem with my methodology for testing this, I would appreciate it being pointed out.#!/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; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Analyzing Randomization Results
by Russ (Deacon) on May 19, 2000 at 04:17 UTC |