Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Add a fixed number of unique elements to hash [sample() vs. shuffle()]

by kcott (Archbishop)
on Mar 05, 2023 at 22:20 UTC ( [id://11150765]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Add a fixed number of unique elements to hash
in thread Add a fixed number of unique elements to hash

G'day tybalt89,

When initially writing the code in my response, I checked the List::Util::shuffle() documentation and noticed the sample() function immediately after it. I hadn't encountered that previously and decided to give it a go.

Curiously, although it did work as documented, multiple runs produced the same results. As you can see from my "sample runs" using shuffle(), multiple runs produced different results (yes, only two runs shown, but I did run it quite a few times).

Looking at source/lib/List/AllUtils.pm, List::AllUtils::sample() should be identical to List::Util::sample(). I have List::Util v1.62 and Perl v5.36.0.

I'm a bit short on time this morning; I was thinking of investigating further this afternoon [Aussie timezone: UTC+11:00]. If you have any insights into the behaviour of sample(), please share.

It did seem like sample(), taking a random selection from the array, was probably a better choice than shuffle(), randomising the entire array and then taking a slice (obviously, benchmarking needed to confirm this). On the down side, sample() requires List::Util v1.54: you'd need at least Perl v5.32.0 (which has v1.55) or an upgrade from CPAN.

— Ken

Replies are listed 'Best First'.
Re^4: Add a fixed number of unique elements to hash [sample() vs. shuffle()]
by hippo (Bishop) on Mar 07, 2023 at 10:50 UTC

    I have a slightly newer verison of List::Util here (1.63 as below) and sample appears to work as intended, ie. different runs produce different results:

    $ pmv List::Util 1.63 $ perl -MList::Util=sample -wE 'my @x=("a".."z"); say sample (3, @x)' jso $ perl -MList::Util=sample -wE 'my @x=("a".."z"); say sample (3, @x)' ter $ perl -MList::Util=sample -wE 'my @x=("a".."z"); say sample (3, @x)' mgl $ perl -MList::Util=sample -wE 'my @x=("a".."z"); say sample (3, @x)' ago $

    There's nothing in the List::Util changelog to indicate any modification to sample so either some other fix has had an effect or something else is at play.


    🦛

      G'day hippo,

      ++ Thanks for checking that out. I ran a verbatim copy of your one-liner four times and got a different result each time; so, on the face of it, this seems to be working correctly (with v1.62 & v1.63).

      I did actually sit down to run some tests yesterday; unfortunately, real-life barged in and the testing needed to take a backseat. I will get to it eventually.

      I'm fairly certain that I didn't change any logic when moving from sample to shuffle. I recall only a very minor modification; probably just these two lines (off the top of my head):

      #use List::Util 'sample'; use List::Util 'shuffle'; ... #my @playlist = sample $list_length, @all_files; my @playlist = (shuffle @all_files)[0 .. $list_length - 1];

      I'll post more when I get to the testing. It's 11pm here now, so that won't be tonight. :-)

      — Ken

Re^4: Add a fixed number of unique elements to hash [sample() vs. shuffle()]
by Vasek (Acolyte) on Mar 05, 2023 at 23:13 UTC
    Many thanks for your replies and testing, but the target machine is running Perl 5.004 (unfortunately I wasn't precise enough when I just wrote Perl 5). The final solution was to use splice as suggested by jwkrahn in the next post.

      What kind of machine are you running Perl 5.004 on?

      Seriously, you've peaqued my curiosity. You have some old desktop system from 1998 up and running? Anything newer than that, you would have upgraded to a more modern version of Perl long ago. Perl 5.004 is 25 years old.


      Dave

        Hi Dave, yes I have an old O2 computer with the amazing irix 6.5.22 operating system. At my first IT job these machines were still used for oil pipe design (along with some other SGI machine, eg. Indigo, Octan and Indy) and when they were scrapped I couldn't resist the temptation to buy a few (Indy and O2). The last still working Indy is now in the attic, but I still use an O2 in my carpentry workshop for listening to music :-)

        hippo thx for the link you shared!

        Best whishes, Vasek
        You have some old desktop system from 1998 up and running?

        In the OP Vasek did say it was an SGI O2 so the date range fits. They were nice machines, if a bit temperamental.


        🦛

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-23 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found