title: Creating random binary files
body: <p>I'm trying to use python to create a random binary file. This is what I've got already:</p>
  
  <pre><code>f = open(filename,'wb')
  for i in xrange(size_kb):
      for ii in xrange(1024/4):
          f.write(struct.pack("=I",random.randint(0,sys.maxint*2+1)))
  
  f.close()
  </code></pre>
  
  <p>But it's terribly slow (0.82 seconds for size_kb=1024 on my 3.9GHz SSD disk machine). A big bottleneck seems to be the random int generation (replacing the randint() with a 0 reduces running time from 0.82s to 0.14s).</p>
  
  <p>Now I know there are more efficient ways of creating random data files (namely dd if=/dev/urandom) but I'm trying to figure this out for sake of curiosity... is there an obvious way to improve this?</p>