in reply to Re: Fisher-Yates Shuffle
in thread Fisher-Yates Shuffle
You might also consider this version:
sub bukShuffle { my( $r, $t ); $r = $_ + rand( @array - $_ ), $t = $array[ $_ ], $array[ $_ ] = $array[ $r ], $array[ $r ] = $t for 0 .. $#array; }
And I offer this method (drawing heavily upon a post by Abigail-II) for checking your modifications produce fair results:
my %h; bukShuffle( @array = qw[a b c d ] ), ++$h{ join ' ', @array } for 1 .. + 1e6; pp %h;
Which for the code above produces:
[19:07:24.88] C:\test>junk50 ( "c a d b", 41868, "a d b c", 41833, "b d a c", 41554, "d a c b", 41772, "b c d a", 41671, "c d a b", 41779, "b d c a", 41783, "a b d c", 41724, "c b a d", 42012, "c a b d", 41571, "b c a d", 41270, "b a c d", 41192, "d c a b", 41443, "a b c d", 41669, "a c d b", 41884, "d b a c", 41786, "c b d a", 41608, "a d c b", 41598, "d c b a", 41544, "b a d c", 41578, "d b c a", 41684, "d a b c", 41672, "a c b d", 41622, "c d b a", 41883, )
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Fisher-Yates Shuffle
by Random_Walk (Prior) on Jun 04, 2014 at 14:38 UTC |