title: Fill huge file with random data quickly
body: <p>I'm trying to fill file of enormous size (>1GB) with random data. </p>
  
  <p>I've written simple "thread safe random", that generates strings (solution was suggested at <a href="https://devblogs.microsoft.com/pfxteam/getting-random-numbers-in-a-thread-safe-way/" rel="nofollow noreferrer">https://devblogs.microsoft.com/pfxteam/getting-random-numbers-in-a-thread-safe-way/</a>), and reworking random to make random strings is trivial.</p>
  
  <p>I'm trying to write this to file using this code:</p>
  
  <pre><code>String rp;
  
  Parallel.For(1, numlines -1, i =&gt; 
  {
      rp = ThreadSafeRandom.Next();
      outputFile.WriteLineAsync(rp.ToString()).Wait();
  });
  </code></pre>
  
  <p>when line numbers are small file is generated perfectly.</p>
  
  <p>When I enter bigger number of lines (say 30000) following happens:</p>
  
  <ul>
  <li><p>some strings are corrupted (Notepad++ sees them as prepended by lots of NUL)</p></li>
  <li><p>at some point i get InvalidOperationException("Thread is used by previous thread operation").</p></li>
  </ul>
  
  <p>I tried making <code>Parallel.For(1, numlines -1, async i =&gt;</code> with <code>await outputFile.WriteLineAsync(rp.ToString());</code></p>
  
  <p>and also tried doing </p>
  
  <pre class="lang-cs prettyprint-override"><code>lock (outputFile) {
      outputFile.WriteLineAsync(rp.ToString());
  }
  </code></pre>
  
  <p>I can always use single thread approach with simple for and simple writeLine() but as I've said I want to generate big files and I assume that even simple for loop that generates > 10000 records can take some time (in file with big size we will have 1e+6 or even 1e9 records, which is > then 20GB) and I can not think about any optimal approach.</p>
  
  <p>Can someone suggest how to optimize this?</p>