Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

randint and --int option.

  • Loading branch information...
commit c8cdc172f3e81d2a1dd898dc67616618b5617913 1 parent 025de93
@ralphbean ralphbean authored committed
Showing with 26 additions and 1 deletion.
  1. +10 −0 quantumrandom/__init__.py
  2. +16 −1 quantumrandom/cmd.py
View
10 quantumrandom/__init__.py
@@ -23,6 +23,7 @@
http://physics0054.anu.edu.au
"""
+import math
import urllib
import urllib2
import binascii
@@ -71,6 +72,15 @@ def hex(array_length=100, block_size=100):
"""Return a chunk of hex"""
return ''.join(get_data('hex16', array_length, block_size))
+def randint(min=0, max=10):
+ """Return an int between min and max"""
+ val = get_data(data_type='uint16', array_length=1, block_size=1)[0]
+ # Normalize
+ val = val / 65536.0
+ # Transform
+ val = int(math.floor(val * (max-min) + min))
+ return str(val)
+
def uint16(array_length=100):
"""Return a numpy array with 100 uint16 numbers"""
View
17 quantumrandom/cmd.py
@@ -25,13 +25,28 @@
import quantumrandom
def main():
+ usage = "Usage: %s [--binary|--hex|--int --min MIN --max MAX]" % \
+ sys.argv[0]
generator = None
+ # TODO -- use argparse here
if '--binary' in sys.argv or '-b' in sys.argv:
generator = quantumrandom.binary
if '--hex' in sys.argv or '-h' in sys.argv:
generator = quantumrandom.hex
+ if '--int' in sys.argv or '-i' in sys.argv:
+ # Special case. Just print one.
+ try:
+ min = int(sys.argv[sys.argv.index('--min')+1])
+ max = int(sys.argv[sys.argv.index('--max')+1])
+ except ValueError:
+ print usage
+ sys.exit(1)
+
+ print quantumrandom.randint(min=min, max=max)
+ sys.exit(0)
+
if not generator:
- print "Usage: %s [--binary|--hex]" % sys.argv[0]
+ print usage
sys.exit(1)
try:
while True:
Please sign in to comment.
Something went wrong with that request. Please try again.