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?? |
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
In Section
Seekers of Perl Wisdom
|
|