Acme-CPANModules-PickingRandomItemsFromList
view release on metacpan or search on metacpan
lib/Acme/CPANModules/PickingRandomItemsFromList.pm view on Meta::CPAN
**2b. Picking multiple items, with equal probability, duplicates not allowed**
If you do not want to allow duplicates, there are several alternatives:
<pm:List::Util> (from version 1.54, 2020-02-02) provides `sample()`. If you use
an older version, you can use `shuffle()` then get as many number of samples as
you need from the first elements of the array using slice
(`@shuffled[0..$num_wanted-1]`) or `head()`.
<pm:List::MoreUtils> also provides `samples()`.
Keywords: sample, sampling.
**3. Picking item(s), with weights**
If you want to assign different weights to different items (so one item might be
picked more likely), you can use one of these modules:
<pm:Array::Sample::WeightedRandom> offers sampling without replacement (not
allowing duplicates) or with replacement (allowing duplicates).
<pm:Random::Skew>.
<pm:Data::Random::Weighted> currently can only pick a single item.
**Tangentially-related modules**
<pm:App::PickArgs> provides CLI <prog:pick-args> to pick random items from
command-line arguments.
MARKDOWN
tags => ['task', 'sampling', 'random'],
entries => [
{
module=>'List::Util',
},
{
module=>'List::MoreUtils',
},
{
module=>'Array::Sample::WeightedRandom',
},
{
module=>'Random::Skew',
},
{
module=>'Data::Random::Weighted',
},
],
};
1;
# ABSTRACT: List of modules to pick random items from a list
__END__
=pod
=encoding UTF-8
=head1 NAME
Acme::CPANModules::PickingRandomItemsFromList - List of modules to pick random items from a list
=head1 VERSION
This document describes version 0.006 of Acme::CPANModules::PickingRandomItemsFromList (from Perl distribution Acme-CPANModules-PickingRandomItemsFromList), released on 2025-05-22.
=head1 DESCRIPTION
If you are picking random lines from a file, there's a separate CPANModules list
for it: L<Acme::CPANModules::PickingRandomLinesFromFile>.
B<1. Picking a single item, with equal probability>
If you only want to pick a single item, with equal probability, you can simply
get a random element from an array using the C<$ary[rand @ary]> idiom.
B<2. Picking multiple items, with equal probability>
B<2a. Picking multiple items, with equal probability, duplicates allowed>
If you want to allow duplicates, you can repeatedly pick random elements from an
array using the C<$ary[rand @ary]> idiom.
B<2b. Picking multiple items, with equal probability, duplicates not allowed>
If you do not want to allow duplicates, there are several alternatives:
L<List::Util> (from version 1.54, 2020-02-02) provides C<sample()>. If you use
an older version, you can use C<shuffle()> then get as many number of samples as
you need from the first elements of the array using slice
(C<@shuffled[0..$num_wanted-1]>) or C<head()>.
L<List::MoreUtils> also provides C<samples()>.
Keywords: sample, sampling.
B<3. Picking item(s), with weights>
If you want to assign different weights to different items (so one item might be
picked more likely), you can use one of these modules:
L<Array::Sample::WeightedRandom> offers sampling without replacement (not
allowing duplicates) or with replacement (allowing duplicates).
L<Random::Skew>.
L<Data::Random::Weighted> currently can only pick a single item.
B<Tangentially-related modules>
L<App::PickArgs> provides CLI L<pick-args> to pick random items from
command-line arguments.
=head1 ACME::CPANMODULES ENTRIES
=over
( run in 0.363 second using v1.01-cache-2.11-cpan-d7f47b0818f )