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