<?xml version="1.0" encoding="windows-1252"?>
<node id="303846" title="Re: Randomize lines with limited memory" created="2003-11-01 17:23:10" updated="2005-06-15 02:50:11">
<type id="11">
note</type>
<author id="5348">
Corion</author>
<data>
<field name="doctext">
&lt;p&gt;You can simply trade time for space here:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Count the number of lines in your text file&lt;/li&gt;
  &lt;li&gt;Generate a randomized order of lines by choosing a permutation of the line numbers (this will take about 32 bytes per line number).&lt;/li&gt;
  &lt;li&gt;Rewind your text file.&lt;/li&gt;
  &lt;li&gt;Read up to the line that is to become the next line written. &lt;/li&gt;
  &lt;li&gt;Copy it into the target file.&lt;/li&gt;
  &lt;li&gt;Repeat the three previous steps until the shuffled file is written out.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Of course, this method won't work well if you always choose the same permutation, but you could also do a Fisher-Yates shuffle of the line numbers and then recreate the file from the line numbers as above.&lt;/p&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-5348"&gt;
&lt;small&gt;&lt;code&gt;
perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ;    # The  
$d = new HTTP::Daemon and fork and getprint $d-&gt;url and exit;#spider
($c = $d-&gt;accept())-&gt;get_request(); $c-&gt;send_response( new   #in the
HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' #  web 
&lt;/code&gt;&lt;/small&gt;

&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
303841</field>
<field name="parent_node">
303841</field>
</data>
</node>
