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?? |
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.
In Section
Seekers of Perl Wisdom
|
|