in reply to Randomising an array

You need to know this: The shuffle() routine implemented in List::Util is a fair shuffle; It is an implementation of the fisher-yates shuffle. Other recipies such as yours are not certain to be 'fair'. I don't know if yours is biased or not, but why leave it to chance when there's a perfectly good alternative built into a module that comes with Perl? I do know that it is very easy to get it wrong when writing an unbiased shuffle routine. Good thing someone included a fair, unbiased shuffle in List::Util


Dave

Replies are listed 'Best First'.
Re^2: Randomising an array
by blokhead (Monsignor) on Aug 04, 2006 at 16:19 UTC
    The OP's shuffle does have a bug. It should have rand scalar @arr instead of rand $#arr. But if that is fixed (and "20" and "200" are changed to match each other), then it is a fair shuffle. However the OP's approach is still inferior to Fisher-Yates in that it has quadratic instead of linear running time (because of the splicing), and it does not shuffle in place like F-Y.

    blokhead