Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://143155]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2025-05-16 17:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.