Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Re: picking multiple random elements out of an array

by dws (Chancellor)
on Feb 04, 2002 at 04:09 UTC ( [id://143155] : note . print w/replies, xml ) Need Help??


in reply to Re: picking multiple random elements out of an array
in thread picking multiple random elements out of an array

Here's a "variation of the above" code fragment from an ancient script. It produces a set of numbers which can be used as indices in an array slice.
# @indices = randomsubset(scalar @array, $size_of_subset); sub randomsubset { my @indices = 0 .. $_[0] - 1; my @subset = (); push @subset, splice(@d, int rand @indices, 1) for 1 .. @_[1]; @subset; } print join (" ", randomsubset(50,5)), "\n";
(The overly specific name indicates sloppy thinking on my part, for which I plead guilty.)

IIRC, there's something equivalent in the Cookbook.