sample#
Pick $count distinct elements from the list at random.
Synopsis#
my @picks = sample $count, @values;
my @three = sample 3, 1..100;
What you get back#
A list of at most $count elements drawn without replacement. If
$count is larger than the input, every element is returned in a
random order — i.e. the function degrades to shuffle. The random
source is $List::Util::RAND if set, otherwise perl’s built-in
rand().
Examples#
my @preview = sample 5, @articles;
my ($winner) = sample 1, @entries;
my @all = sample 999, qw( a b c ); # ('c','a','b') say
Edge cases#
$count <= 0returns the empty list.Fewer inputs than
$countreturns all inputs, shuffled.
Differences from upstream#
Fully compatible with upstream.
See also#
shuffle— permute the whole list.head— deterministic prefix (no randomness).