Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
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?
[LanX]: ha ... just read an article saying that some US presidents in the past lost their nuclear codes
[LanX]: xD
[Corion]: The code is 00000000, as always, no?
[GotToBTru]: I remember hearing about somebody misplacing a list of code phrases or something
[marto]: 12345
[GotToBTru]: the "nuclear codes" are in the case cuffed to the wrist of some guy
[LanX]: not always

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2017-01-20 12:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (174 votes). Check out past polls.