Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

poor man's shuffle :)

by LanX (Canon)
on Mar 27, 2013 at 19:38 UTC ( #1025797=note: print w/ replies, xml ) Need Help??


in reply to Re: Curious find while comparing grep, map, and smart match...
in thread Curious find while comparing grep, map, and smart match...

Just for fun, here a poor mans shuffle:

DB<228> sort { (-1)**(rand(2)%2) } 1..10 => (5, 8, 7, 6, 9, 10, 1, 4, 2, 3)

Surely neither efficient, nor well mixed and pretty pointless since List::Util is core. =)

Cheers Rolf

( addicted to the Perl Programming Language)

update

for completeness:

> nor well mixed

The distribution is perfect iff the list has 2^n elements

DB<483> for (1..100000) {$x=0;$g[$x++]{$_}++ for sort {(-1) ** int r +and 2} a..d } => "" DB<484> \@g => [ { a => 25302, b => 25028, c => 24852, d => 24818 }, { a => 25062, b => 24925, c => 24773, d => 25240 }, { a => 24773, b => 24944, c => 25203, d => 25080 }, { a => 24863, b => 25103, c => 25172, d => 24862 }, ]


Comment on poor man's shuffle :)
Select or Download Code
Re: poor man's shuffle :)
by BrowserUk (Pope) on Mar 27, 2013 at 19:52 UTC
    Surely neither efficient, nor well mixed and pretty pointless ...

    Agreed.

      > Agreed.

      =)

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (17)
As of 2015-07-01 20:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (19 votes), past polls