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?? |
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:
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().
Here's some sample runs (the updated script is called "rand_select_files_sample.pl"):
Clearly, multiple runs are producing different results. I don't have any results from earlier runs using sample(). Assumption: PEBCAC — Ken
In Section
Seekers of Perl Wisdom
|
|