Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
for $r (0..5000) {
@a=sort { int(rand(2))?1:-1 } (0..9);
($g)=grep($a$_==4,(0..9));
$h{$g}++;
}
for $r (0..9) {print "$r $h{$r}\n";}
As you can see if you run this, the element originally in the fifth position has a much larger chance of staying put or of moving only slightly than it should if the list were truly randomly shuffled.
@b=(0..9); @a=();
while(@b) {$i=int(rand($#b+1)); push @a, splice(@b,$i,1);}
Unfortunately, this seems awfully slow.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Randomly sorted array
by ZZamboni (Curate) on Apr 27, 2000 at 17:23 UTC | |
|
Re: Randomly sorted array
by snowcrash (Friar) on Apr 27, 2000 at 17:30 UTC | |
|
RE: Randomly sorted array
by lhoward (Vicar) on Apr 28, 2000 at 17:10 UTC |