Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Random array sorting

by bsoisoi
on Dec 05, 2007 at 17:35 UTC ( #655140=note: print w/ replies, xml ) Need Help??


in reply to •Re: Random array sorting
in thread Random array sorting

I have an even easier way.
my @array = ('one', 'two', 4, 5, 23242, 'all the candy in the universe', 'zebras');
print $_, "\n" foreach (sort {int(rand(3))-1} @array);
Just makeup the -1, 0, 1 digits that $a cmp $b would return.


Comment on Re^2: Random array sorting
Re^3: Random array sorting
by merlyn (Sage) on Dec 05, 2007 at 18:31 UTC
    That's been shown time and time again to not be a "fair" shuffle, and in earlier Perl versions, would actually lose data or even core-dump. Bad. And yeah, I tried it in order to find that out.
Re^3: Random array sorting
by ikegami (Pope) on Dec 05, 2007 at 18:41 UTC

    Ignoring that it doesn't work, I don't see how

    print $_, "\n" foreach (sort {int(rand(3))-1} @array);

    is easier than

    print $_, "\n" foreach shuffle @array;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://655140]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2014-09-22 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (198 votes), past polls