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