| 1 | package Acme::CPANModules::PickingRandomItemsFromList; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |
| 6 | 1 | our $DATE = '2023-06-20'; # DATE | |
| 7 | 1 | our $DIST = 'Acme-CPANModules-PickingRandomItemsFromList'; # DIST | |
| 8 | 1 | our $VERSION = '0.004'; # VERSION | |
| 9 | |||
| 10 | 1 | our $LIST = { | |
| 11 | summary => 'Picking random items from a list', | ||
| 12 | description => <<'_', | ||
| 13 | |||
| 14 | If you are picking random lines from a file, there's a separate CPANModules list | ||
| 15 | for it: <pm:Acme::CPANModules::PickingRandomLinesFromFile>. | ||
| 16 | |||
| 17 | **1. Picking a single item, with equal probability** | ||
| 18 | |||
| 19 | If you only want to pick a single item, with equal probability, you can simply | ||
| 20 | get a random element from an array using the `$ary[rand @ary]` idiom. | ||
| 21 | |||
| 22 | **2. Picking multiple items, with equal probability** | ||
| 23 | |||
| 24 | **2a. Picking multiple items, with equal probability, duplicates allowed** | ||
| 25 | |||
| 26 | If you want to allow duplicates, you can repeatedly pick random elements from an | ||
| 27 | array using the `$ary[rand @ary]` idiom. | ||
| 28 | |||
| 29 | **2b. Picking multiple items, with equal probability, duplicates not allowed** | ||
| 30 | |||
| 31 | If you do not want to allow duplicates, there are several alternatives: | ||
| 32 | |||
| 33 | <pm:List::Util> (from version 1.54, 2020-02-02) provides `sample()`. If you use | ||
| 34 | an older version, you can use `shuffle()` then get as many number of samples as | ||
| 35 | you need from the first elements of the array using slice | ||
| 36 | (`@shuffled[0..$num_wanted-1]`) or `head()`. | ||
| 37 | |||
| 38 | <pm:List::MoreUtils> also provides `samples()`. | ||
| 39 | |||
| 40 | Keywords: sample, sampling. | ||
| 41 | |||
| 42 | **3. Picking item(s), with weights** | ||
| 43 | |||
| 44 | If you want to assign different weights to different items (so one item might be | ||
| 45 | picked more likely), you can use one of these modules: | ||
| 46 | |||
| 47 | <pm:Array::Sample::WeightedRandom> offers sampling without replacement (not | ||
| 48 | allowing duplicates) or with replacement (allowing duplicates). | ||
| 49 | |||
| 50 | <pm:Random::Skew>. | ||
| 51 | |||
| 52 | <pm:Data::Random::Weighted> currently can only pick a single item. | ||
| 53 | |||
| 54 | _ | ||
| 55 | tags => ['task', 'sampling', 'random'], | ||
| 56 | entries => [ | ||
| 57 | { | ||
| 58 | module=>'List::Util', | ||
| 59 | }, | ||
| 60 | { | ||
| 61 | module=>'List::MoreUtils', | ||
| 62 | }, | ||
| 63 | { | ||
| 64 | module=>'Array::Sample::WeightedRandom', | ||
| 65 | }, | ||
| 66 | { | ||
| 67 | module=>'Random::Skew', | ||
| 68 | }, | ||
| 69 | { | ||
| 70 | module=>'Data::Random::Weighted', | ||
| 71 | }, | ||
| 72 | ], | ||
| 73 | }; | ||
| 74 | |||
| 75 | 1 | 1; | |
| 76 | # ABSTRACT: Picking random items from a list | ||
| 77 | |||
| 78 | __END__ | ||