title: how to generate 1 million random integers and write them to a file?
body: <p>I was trying to do some tests on my external sorting algorithms, and I thought I should generate a huge amount of random numbers and put them into a file.</p>
  
  <p>Here is how I do it:</p>
  
  <pre><code>import tempfile, random
  
  nf = tempfile.NamedTemporaryFile(delete=False)
  i = 0
  while i &lt; 1000:
      j = 0
      buf = ''
      while j &lt; 1000:
          buf += str(random.randint(0, 1000))
          j += 1
      nf.write(buf)
      i += 1
  </code></pre>
  
  <p>I thought, I should speed up the generating process by <em>reducing the File IO operations</em>, so I use <code>buf</code> to store as many numbers as possible, then write buf to the file.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>I still got a sense that, the generating and writing process was slow.</p>
  
  <p>Am I getting something wrong?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>In C++, we can simply write an <code>int</code> or a <code>float</code> into file by <code>&lt;&lt;</code> without <code>converting them</code> into string.</p>
  
  <p>So can we do the same in Python? I mean write an integer into file without converting it into str.</p>