<?xml version="1.0" encoding="windows-1252"?>
<node id="820" title="How do I select a random line from a file?" created="1999-10-12 23:42:27" updated="2005-08-14 03:10:18">
<type id="834">
perlfaq nodetype</type>
<author id="519">
faq_monk</author>
<data>
<field name="doctext">


&lt;P&gt;
Here's an algorithm from the Camel Book:

&lt;P&gt;
&lt;PRE&gt;    srand;
    rand($.) &amp;lt; 1 &amp;amp;&amp;amp; ($line = $_) while &amp;lt;&amp;gt;;
&lt;/PRE&gt;
&lt;P&gt;
This has a significant advantage in space over reading the whole file in. 
&lt;FONT SIZE=-1&gt;A&lt;/FONT&gt; simple proof by induction is available upon request
if you doubt its correctness.

&lt;P&gt;
</field>
</data>
</node>
