Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: How do I pick a random line from a file?

by vroom (His Eminence)
on Jan 09, 2000 at 10:38 UTC ( [id://1911]=note: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    1. read line 1
    2. rand(1)<1    #100% chance it's true if we had just one line
    ...
    4. rand(2)<1    #50% chance that line 2 replaces line 1 as the random 
    +line.
    5. read line 3
    6. rand(3)<1    #1/3 chance that line 3 replaces the currently chosen 
    +line
    
  2. or download this
    srand;
    open FILE, "<filename" or die "Could not open filename: $!\n";
    rand($.)<1 and ($line=$_) while <FILE>;
    close FILE;
    print "Random line is $line\n";
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-25 05:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found