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

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.


🦛

Replies are listed 'Best First'.
Re^5: Add a fixed number of unique elements to hash [sample() vs. shuffle()]
by kcott (Archbishop) on Mar 07, 2023 at 12:07 UTC

    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