in reply to Re: reading a random line from a file
in thread reading a random line from a file
How do I select a random line from a file?
Here's an algorithm from the Camel Book:
srand; rand($.) < 1 && ($line = $_) while <>;
This has a significant advantage in space over reading the whole file in. A simple proof by induction is available upon request if you doubt the algorithm's correctness.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: reading a random line from a file
by BrowserUk (Patriarch) on Nov 17, 2002 at 20:57 UTC | |
|
Re^3: reading a random line from a file
by Aristotle (Chancellor) on Nov 18, 2002 at 00:12 UTC |