Skip to content

Instantly share code, notes, and snippets.

@Zhangerr
Created January 2, 2013 04:09
Show Gist options
  • Save Zhangerr/4432074 to your computer and use it in GitHub Desktop.
Save Zhangerr/4432074 to your computer and use it in GitHub Desktop.
simple script for generating random numbers in python
#!/usr/bin/python
import sys
import getopt
n = 1
src = "urandom"
def Usage():
print """a simple script to generate random numbers. syntax is as follows:
""" + sys.argv[0]+ """ [options] <number of random numbers to print>
There are only 2 options:
-r, --random use the "more" random but blocking /dev/random (quite slow)
-h, --help prints this help
""",
# Get options
try:
optlist, args = getopt.getopt(sys.argv[1:], 'hr', ['help','random'])
except KeyboardInterrupt:
sys.exit("KeyboardInterrupt caught.")
except getopt.GetoptError:
print "Invalid arguments!"
Usage()
sys.exit(1)
#print optlist
#print args
for opt, arg in optlist:
if opt == "-h" or opt == "--help":
Usage()
sys.exit(0);
elif opt == "-r" or opt == "--random":
src="random"
if(len(args) > 0):
try:
n = int(args[0])
except ValueError:
print "Please enter a valid numerical argument."
sys.exit(1)
f = open("/dev/"+src, "rb")
for i in xrange(0,n):
print str(ord(f.read(1)))+' ',
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment