Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: picking multiple random elements out of an array

by chipmunk (Parson)
on Feb 04, 2002 at 07:05 UTC ( #143173=note: print w/replies, xml ) Need Help??


in reply to picking multiple random elements out of an array

Here's an extension of the FAQ's method for selecting a random line from a file. This one selects multiple lines/entries. It keeps the selected entries in their original order.
my $count = 5; my @select; my $i = 0; for (@array) { push @select, $_ if rand() < ($count / ++$i); if (@select > $count) { splice(@select, rand($#select), 1); } }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://143173]
help
Chatterbox?
[1nickt]: Just don't make it barf.
[1nickt]: perldigious that's exactly what my stepson says.
[1nickt]: the tick that can make you a vegetarian
[perldigious]: Your Mother: Yeah, I have a tool for ticks that I use (mostly on my dogs thankfully), but it's just a cheapo one I got from my vets office... it works pretty good though.
erix saw a tick in a natural history museum collected from the scrotum of the curator (it was removed)
[Your Mother]: The exhibit, the tick, or the scrotum?
[erix]: (the man is even a paleontologist )
[1nickt]: erix unfortunately that's there favourite spot. That's why they are usually spotted crawling *up* your leg.
[erix]: ha, the tick and, as it happens, also the curator (now a professor of Paleont.)
[1nickt]: s/there/their/, ugh sorry.

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (12)
As of 2017-05-24 13:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?