Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by kcott (Archbishop)
on Mar 23, 2023 at 01:43 UTC ( [id://11151147]=note: print w/replies, xml ) Need Help??


in reply to Add a fixed number of unique elements to hash

TL;DR sample() works fine.

Background: I had supplied a solution 2-3 weeks ago. I then questioned whether sample() would've been better than shuffle(). I indicated that I'd encountered a potential problem; but real-life got in the way of further testing. I've just noticed that I didn't follow-up on that when real-life got out of the way. :-)

For ease of reference, here's my previous posts which are somewhat scattered throughout the thread:

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

The OP subsequently wrote that a solution was required that could be used with Perl 5.004; accordingly, this response generally addresses the issue but will not be specifically useful to the OP.

I made two modifications to the code posted in "Re: Add a fixed number of unique elements to hash" that change shuffle() to sample(). I also removed a sort to better highlight the randomness of sample().

... #use List::Util 'shuffle'; use List::Util 'sample'; ... #my @playlist = (shuffle @all_files)[0 .. $list_length - 1]; my @playlist = sample $list_length, @all_files; #print "$_\n" for sort @playlist; print "$_\n" for @playlist; ...

Here's some sample runs (the updated script is called "rand_select_files_sample.pl"):

ken@titan ~/tmp/pm_11150231_dir_rand_select $ ./rand_select_files_sample.pl /home/ken/tmp/pm_11150231_dir_rand_select/inc2/n.x /home/ken/tmp/pm_11150231_dir_rand_select/a.x /home/ken/tmp/pm_11150231_dir_rand_select/c.z ken@titan ~/tmp/pm_11150231_dir_rand_select $ ./rand_select_files_sample.pl /home/ken/tmp/pm_11150231_dir_rand_select/inc1/j.z /home/ken/tmp/pm_11150231_dir_rand_select/c.z /home/ken/tmp/pm_11150231_dir_rand_select/inc1/h.x ken@titan ~/tmp/pm_11150231_dir_rand_select $ ./rand_select_files_sample.pl /home/ken/tmp/pm_11150231_dir_rand_select/inc1/j.z /home/ken/tmp/pm_11150231_dir_rand_select/c.z /home/ken/tmp/pm_11150231_dir_rand_select/a.x

Clearly, multiple runs are producing different results. I don't have any results from earlier runs using sample(). Assumption: PEBCAC

— Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2024-04-26 08:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found