What kind of constraints do you have? Does each process needs to get the same amount of bytes? The same amount of lines? Or if you have N processes, will approx. 1/N of the lines do? Can the lines be fed in the same order as in the file, or does that have to be random as well? Can you just chop the file into N equal pieces and shuffle those? If not, why not? Would the following algorithm do?
  1. Let the number of lines in the file be L. Let the number of processes be N. Let Ai = 0 for 0 <= i < N. Label the processes P_i, 0 <= i < N.
  2. For each line l, 0 <= l < L, assign this line to process P_i with chance (L/N - A_i)/(L - l). For the process P_j which is picked this way, increment A_j.
  3. If necessary, shuffle the lines assigned to each process.

Some pseudo, untested, code:

my $L = ... number of lines ....; my $N = ... number of processes ....; # Open filehandles for each process: my @fh; for (my $i = 0; $i < $N; $i ++) { open $fh[$i], "| $process" or die; } # Initialize @A: my @A = (0) x $N; my $l = 0; # Iterate over the input. while (<$input>) { # Pick a number. my $r = rand($L - $l); # Find process. my $i = 0; while ($r >= ($L / $N - $A[$i])) { $r -= $L / $N - $A[$i]; $i ++; } # Write line. print $fh[$i] $_; # Increment array. $A[$i]++; # Increment line counter; $l++ }

In reply to Re: Randomizing Big Files by Anonymous Monk
in thread Randomizing Big Files by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.