Skip to content

Instantly share code, notes, and snippets.

@santa4nt
Last active December 16, 2015 08:19
Show Gist options
  • Select an option

  • Save santa4nt/5405351 to your computer and use it in GitHub Desktop.

Select an option

Save santa4nt/5405351 to your computer and use it in GitHub Desktop.
Generate one million random, signed 32-bit integers.
#!/usr/bin/env python
import os
import sys
import array
import struct
NUMRANDS = 10 ** 6 # the number of random integers we want to generate (one million)
BUFSIZE = 1000 # the number of integers we buffer internally before flushing to output
a = array.array('i')
for i in xrange(NUMRANDS): # generate one million 32-bit integers
# generate the integer (signed) from the OS' random source of byte strings
randint = struct.unpack('@i', os.urandom(4))[0]
a.append(randint) # buffer the random integer into an array
if len(a) >= BUFSIZE: # flush to output buffer once we have enough
a.tofile(sys.stdout)
del a[:]
if a:
a.tofile(sys.stdout)
@santa4nt
Copy link
Author

$ ./genrandints.py > randints
$ du -b --si randints
4.0M    randints

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment