tamaguchi has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Randomising an array
by Limbic~Region (Chancellor) on Aug 04, 2006 at 12:43 UTC
    tamaguchi,
    This is a FAQ but the short answer is to use List::Util's shuffle() function. If you are interested in other ways then search the FAQs ;-)

    Cheers - L~R

Re: Randomising an array
by davorg (Chancellor) on Aug 04, 2006 at 12:57 UTC
      Why don't people read FAQs any more?
      Maybe because they don't know how? :)
      perldoc -q random
      or one could take a look in the Perl FAQ here at PM, sneakily called Categorized Questions and Answers (array->how do I shuffle).

      -- Hofmator

        If you can ask here, you know how to search
Re: Randomising an array
by davido (Cardinal) on Aug 04, 2006 at 14:27 UTC

    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

      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

Re: Randomising an array
by Samy_rio (Vicar) on Aug 04, 2006 at 13:00 UTC

    Hi tamaguchi, Try this,

    use strict; use warnings; use Data::Random qw(:all); my @set = (1..200); my @random_set = rand_set( set => \@set, size => $#set ); $,="\n"; print @random_set;

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: Randomising an array
by planetscape (Chancellor) on Aug 04, 2006 at 21:21 UTC
Re: Randomising an array
by starbolin (Hermit) on Aug 06, 2006 at 03:08 UTC

    The best way to unsort an array? Sort it!!

    @a=0..20;$,=" "; print sort {1-int rand 3} @a;

    OK, I couldn't resist golfing it.

    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}
Re: Randomising an array
by starbolin (Hermit) on Aug 04, 2006 at 16:51 UTC

    If this is for a class project or a nuclear power plant I wouldn't do it this way:

    @a = (0..20);$,="\n"; for(0..200){$b=shift @a;push @a,(int rand 2)?($b,shift @a):(shift@a,$b +)} print @a;


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}