sample#

Pick $count distinct elements from the list at random.

תקציר#

my @picks = sample $count, @values;
my @three = sample 3, 1..100;

מה מוחזר#

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().

דוגמאות#

my @preview = sample 5, @articles;
my ($winner) = sample 1, @entries;
my @all     = sample 999, qw( a b c );   # ('c','a','b') say

מקרי קצה#

  • $count <= 0 returns the empty list.

  • Fewer inputs than $count returns all inputs, shuffled.

הבדלים מן ה-upstream#

Fully compatible with upstream.

ראו גם#

  • shuffle - permute the whole list.

  • head - deterministic prefix (no randomness).