Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Curious find while comparing grep, map, and smart match...

by BrowserUk (Pope)
on Mar 27, 2013 at 02:31 UTC ( #1025617=note: print w/replies, xml ) Need Help??

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

All of your methods of picking 100 random numbers between 1 & 120 are horribly complicated and inefficient:

use List::Util qw[ shuffle ]; my @rands = ( shuffle 1 .. 120 )[ 0 .. 99 ];

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
poor man's shuffle :)
by LanX (Chancellor) on Mar 27, 2013 at 19:38 UTC
    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)


    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 }, ]
      Surely neither efficient, nor well mixed and pretty pointless ...


        > Agreed.


        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re^2: Curious find while comparing grep, map, and smart match...
by dbuckhal (Pilgrim) on Mar 27, 2013 at 04:50 UTC

      I will make an effort to interpret BrowserUKs terse comment for you :-)

      In grepGen/mapGen/smartGen you have a redo in the event of a collision, whereas if you really want unique random numbers, they could be generated much more simply by the shuffle method he suggested. His method does not involve repeated retries and removes the need for any of these functions. His one liner does what each of the three methods you proposed does, but much more efficiently....

      my @rands = ( shuffle # shuffle, like a pack of cards 1 .. 120 # a series of numbers from 1..120 ) [ 0 .. 99 ]; # take the first 100 of these # the resulting @rands is a 100 element array of numbers # in the range 1..120, all of which are unique
      A Monk aims to give answers to those who have none, and to learn from those who know more.

        Thank you for your interpratation. :D I knew what he was trying to say, but in this instance he was wrong. I was measuring each of those functions against a pool of random numbers, where they would each encounter the same set, including duplicates. So, the script was written as I thought is should be to accomplish my goal.

        I always enjoy peeking around here reading the insights presented to everyone's code, even such comments which may not be directly relevant, but target the improving of one's coding skills. But, sometimes a question of what is the goal of the program can be more educational for both sides.

        As always, comments and opinions are appreciated.

      Explain what?

      The code I posted; or why your code is inefficient?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1025617]
[Discipulus]: dear brother karl has "Furthermore I consider that Donald Trump must be impeached as soon as possible" in his sig
[erix]: ah, thanks Discipulus
[1nickt]: erix perhaps you could kindly point out your technique to roho and everyone will be happy.
[Discipulus]: but i'm also animalist, so i' hirted by 1nickt's sig: The way forward always starts with a mammal test... grin..
[erix]: I might if I did remember. It's probably somewhere in the site docs
[uhClem]: I don't favor that sort of thing around here but the place for our politics is wherever we live.
[uhClem]: And besides, the objection to it was off-topic.
[Discipulus]: 1nickt hide signature is a positive action; i think is not a solution here

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (12)
As of 2017-06-22 12:33 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (519 votes). Check out past polls.