Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
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 rifling through the Monastery: (12)
As of 2014-12-18 21:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (66 votes), past polls