in reply to A bad shuffle
Your stats knowledge/understanding seems to be way above mine ... but wouldn't this be just as good:
? I always get kinda concerned about swapping, I don't know why, when a simple random ordering would suffice. But, I didn't really do that well in stats in university, and that was altogether too long ago to remember anyway. :-) Comparing speeds ...sub shuffle { my @in = @_; my @out; push @out, splice(@in, rand @in, 1) while @in; @out; }
which gives:use strict; use Benchmark qw(cmpthese); my @cards = 1..52; cmpthese(-1, { shuffle => sub { shuffle(@cards) }, random_perm => sub { random_perm(@cards) }, }); sub random_perm { my $n = my @p = @_; for ( my $i = $#p; $i > 0; --$i ) { my $j = int rand( $i + 1 ); @p[ $i, $j ] = @p[ $j, $i ]; } return @p; } sub shuffle { my @in = @_; my @out; push @out, splice(@in, rand @in, 1) while @in; @out; }
which says that if random_perm is the optimal pseudo-random shuffler, then my code must be suboptimal in that it's skipping something in making it random. For the life of me, I can't think of what.Rate random_perm shuffle random_perm 6000/s -- -30% shuffle 8606/s 43% --
Update: Changed the benchmark to actually test a real shuffle (woops) - thanks to anonymonk below. Anonymonk points out that this doesn't scale to 52000, which is quite valid for those who need to sort "large" amounts. For those who are only sorting a deck of cards (a very popular subset of shuffling), this may be sufficient ;-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: A bad shuffle
by tlm (Prior) on Mar 20, 2005 at 23:04 UTC | |
|
Re^2: A bad shuffle
by Anonymous Monk on Mar 21, 2005 at 10:04 UTC | |
|
Re^2: A bad shuffle
by Anonymous Monk on Mar 22, 2005 at 09:55 UTC |