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 reply to Re^2: Opening random files then reading random lines from file.
by JavaFan
in thread Opening random files then reading random lines from file.
by fame
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |