8.6. Picking a Random Line from a FileProblemYou want to return a random line from a file. Solution
Use srand; rand($.) < 1 && ($line = $_) while <>; # $line is the random line DiscussionThis is a beautiful example of a solution that may not be obvious. We read every line in the file but don't have to store them all in memory. This is great for large files. Each line has a 1 in N (where N is the number of lines read so far) chance of being selected. Here's a replacement for fortune using this algorithm: $/ = "%%\n"; $data = '/usr/share/games/fortunes'; srand; rand($.) < 1 && ($adage = $_) while <>; print $adage; If you know line offsets (for instance, you've created an index) and the number of lines, you can randomly select a line and jump to its offset in the file, but you usually don't have such an index.
Here's a more rigorous explanation of how the algorithm works. The function call First, some concrete examples, then abstract ones. Obviously, a file with one line (N=1) is fair: you always keep the first line because 1/1 = 100%, making it fair for files of 1 line. For a file with two lines, N=2. You always keep the first line; then when reaching the second line, you have a 50% chance of keeping it. Thus, both lines have an equal chance of being selected, which shows that N=2 is fair. For a file with three lines, N=3. You have a onethird chance, 33%, of keeping that third line. That leaves a twothirds chance of retaining one of the first two out of the three lines. But we've already shown that for those first two lines there's a 5050 chance of selecting either one. 50 percent of twothirds is onethird. Thus, you have a onethird chance of selecting each of the three lines of the file. In the general case, a file of N+1 lines will choose the last line 1/(N+1) times and one of the previous N lines N/(N+1) times. Dividing N/(N+1) by N leaves us with 1/(N+1) for each the first N lines in our N+1 line file, and also 1/(N+1) for line number N+1. The algorithm is therefore fair for all N, where N is a positive integer. We've managed to choose fairly a random line from a file with speed directly proportional to the size of the file, but using no more memory than it takes to hold the longest line, even in the worst case. See Also
The 
