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


In reply to Re: Add a fixed number of unique elements to hash [UPDATE on sample() vs. shuffle()] by kcott
in thread Add a fixed number of unique elements to hash by Vasek

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.