Last active
December 16, 2015 08:19
-
-
Save santa4nt/5405351 to your computer and use it in GitHub Desktop.
Generate one million random, signed 32-bit integers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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) |
Author
santa4nt
commented
Apr 17, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment