matija has asked for the wisdom of the Perl Monks concerning the following question:
I'm working on a project which uses TheSchwartz, and I saw that it was supposed to work with multiple databases.
I was not sure how it would maintain some of it's stuff over more than one database, so I tested it - and found that all the stuff kept going to the same database server.
I looked at the source, and found that it's routine for shuffling databases uses Util::List, and so I tested it:
That gave me:perl -e 'use List::Util qw(shuffle); @a=[1,2,3,4,5,6,7,8,9,10]; foreac +h (1..24) { print join(",",@ { shuffle(@a) })."\n"}'
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
The shuffle routine in List::Util looks reasonable at first glance:
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
Personally, I'm never comfortable using floating point values for array indices, but perhaps there's a perl magic at work that I don't understand. At any rate, either there's a bug in shuffle, or something is very wrong with my random generator - or am I, along with the author of TheSchwartz using List::Util::shuffle incorrectly?sub shuffle (@) { my @a=\(@_); my $n; my $i=@_; map { $n = rand($i--); (${$a[$n]}, $a[$n] = $a[$i])[0]; } @_; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Bug in List::Util::shuffle ?
by friedo (Prior) on Dec 06, 2008 at 14:50 UTC | |
by matija (Priest) on Dec 06, 2008 at 14:54 UTC | |
Re: Bug in List::Util::shuffle ?
by PetaMem (Priest) on Mar 23, 2009 at 08:58 UTC | |
by Corion (Patriarch) on Mar 23, 2009 at 09:09 UTC |
Back to
Seekers of Perl Wisdom