Hi.
Okay, would the maths folk like to offer a helpful explanation?
a
---------- Forwarded message ----------
Question:
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 its correctness.
--
The contents of this message are part of the Perl FAQ:
Please note that "I" below refers to FAQ authors, and *not* Jeff Yoak or Perl FAQ a Day.
Where to get this document [the Perl FAQ]
This document is posted regularly to comp.lang.perl.announce and
several other related newsgroups. It is available in a variety of
formats from CPAN in the /CPAN/doc/FAQs/FAQ/ directory, or on the web
at http://www.perl.com/perl/faq/ .
CREDITS
When I first began the Perl FAQ in the late 80s, I never realized it
would have grown to over a hundred pages, nor that Perl would ever
become so popular and widespread. This document could not have been
written without the tremendous help provided by Larry Wall and the
rest of the Perl Porters.
AUTHOR AND COPYRIGHT INFORMATION
Copyright (c) 1997 Tom Christiansen and Nathan Torkington. All rights
reserved.
--
162
..
Thread Next