Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Opening random files then reading random lines from file.

by JavaFan (Canon)
on Apr 27, 2012 at 08:59 UTC ( [id://967557]=note: print w/replies, xml ) Need Help??


in reply to Re: Opening random files then reading random lines from file.
in thread Opening random files then reading random lines from file.

Since you need to know the number of the lines in the file,
No, you don't. There's a fair and linear way to pick a random element from a stream of unknown length, and its algorithm (which can be implemented as a one-liner) has been displayed in a post before yours.

Detailed analysis can be found in The Art of Computer Programming, by Donald Knuth. Volume 1, IIRC.

  • Comment on Re^2: Opening random files then reading random lines from file.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-19 08:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found