Skip to main content

Tools for utilizing the ANU Quantum Random Numbers Server

https://github.com/lmacken/quantumrandom/actions/workflows/tests.yml/badge.svg?branch=develop https://img.shields.io/pypi/v/quantumrandom.svg https://img.shields.io/pypi/dm/quantumrandom.svg

This project provides tools for interacting with The ANU Quantum Random Number Generator (quantumnumbers.anu.edu.au), which generates true random numbers in real time by measuring the quantum fluctuations of the vacuum. It communicates with their JSON API and provides a qrandom command-line tool and a Python API, including a drop-in replacement for Python’s own random.Random.

quantumrandom works on Python 3.9 and newer, and has no dependencies.

Installing

pip install quantumrandom

Getting an API key

Sign up at quantumnumbers.anu.edu.au to create an API key. At the time of writing the free plan allows 100 requests per month, at up to 1 request per second. A paid plan, billed per request through the AWS Marketplace, has no monthly limit and allows 100 requests per second. See their pricing for the details. quantumrandom works the same way with a key from any plan.

The easiest way to provide your key is with an environment variable, which both the Python API and the qrandom command will pick up:

export QUANTUMRANDOM_API_KEY=your-api-key

You can also set it from Python, which takes precedence over the environment:

>>> import quantumrandom
>>> quantumrandom.set_api_key('your-api-key')

Lastly, anything that makes API requests accepts an api_key argument, for when you need to use more than one key.

Making the most of your requests

ANU counts requests, not the amount of data that they return, and a single request can return anything up to 20480 bytes. quantumrandom is designed around this: it asks for as much as it can at a time, and keeps what you have not used yet in memory so that it does not need to ask again until it runs out. For example, rolling a die a thousand times with quantumrandom.randint(1, 6) costs one request, not a thousand.

Random data is only ever handed out once, and the buffered data lasts only as long as your Python process does.

If a request is rate limited it is retried automatically, after a short pause, up to quantumrandom.MAX_RETRIES (3) times.

Command-line tool

$ qrandom --int --min 5 --max 15
7
$ qrandom --int --min 1 --max 6 --count 5
5
6
4
4
1
$ qrandom --hex
1dc59fde43b5045120453186d45653dd455bd8e6fc7d8c591f0018fa9261ab2835eb210e8
e267cf35a54c02ce2a93b3ec448c4c7aa84fdedb61c7b0d87c9e7acf8e9fdadc8d68bcaa5a
...
$ qrandom --binary > data
$ ls -l data
-rw-r--r-- 1 user user 20480 Nov 24 12:00 data

--hex and --binary output one block of 20480 bytes, at the cost of one API request. Use --count to ask for more than one block (or more than one integer), or --count 0 to keep going until interrupted:

$ qrandom --binary --count 0 | dd of=data

Run qrandom --help for the full list of options.

Python API

The simplest way to get going is with quantumrandom.QuantumRandom, which is a subclass of random.Random that gets its randomness from the quantum random number generator. It supports everything that the random module does.

>>> import quantumrandom
>>> qr = quantumrandom.QuantumRandom()
>>> qr.random()
0.794368699495203
>>> qr.randint(1, 6)
4
>>> qr.choice(['heads', 'tails'])
'tails'
>>> qr.sample(range(1, 50), 6)
[12, 49, 36, 43, 31, 25]
>>> deck = list(range(52))
>>> qr.shuffle(deck)
>>> qr.randbytes(16).hex()
'e686697890ac68804bd8075067edd33d'

Just like random.SystemRandom, it has no state to seed or save, so sequences cannot be reproduced. Each instance keeps its own buffer of random data, and can be given its own api_key.

There is also a randint function at the top level, for convenience. It draws from a buffer that is shared by the whole process.

>>> quantumrandom.randint(0, 20)
5

The low-level get_data function is modelled after the ANU Quantum Random Number Generator’s JSON API. It makes one request, and returns a list of uint8 or uint16 integers, or hex8 or hex16 strings.

>>> quantumrandom.get_data()
[26646]
>>> quantumrandom.get_data(data_type='uint16', array_length=5)
[22546, 58383, 55100, 9936, 55159]
>>> quantumrandom.get_data(data_type='hex8', array_length=3, block_size=2)
['8350', 'df68', 'ae1f']
>>> quantumrandom.get_data(data_type='hex16', array_length=2, block_size=3)
['8ba57d8c7d1e', 'bc54a2eb5a07']

The array_length cannot be larger than 1024. The block_size only applies to the hex data types, where it is the number of 8 or 16 bit values in each string, and cannot be larger than 10.

Based on this get_data function, quantumrandom also provides a few higher-level helper functions. Each of these makes one request when called.

>>> quantumrandom.hex()[:10]
'8272613343'
>>> len(quantumrandom.binary())
20480
>>> quantumrandom.binary(array_length=8, block_size=1)
b'\x07kE\xbb\x7f\xf8\x12\x83\xd5\xc6\xa2\xd17\x8e\x03\x17'
>>> quantumrandom.uint16()
array([24094, 13944, 22109, 22908, 34878, 33797, 47221, 21485, 37930, ...], dtype=uint16)

uint16 requires NumPy, which you can install along with quantumrandom by running pip install quantumrandom[numpy].

cached_generator returns a generator that yields numbers forever, making a request for 1024 more each time it runs out. One can be passed to randint to have it take its numbers from there instead.

>>> generator = quantumrandom.cached_generator()
>>> next(generator)
48013
>>> quantumrandom.randint(1, 6, generator)
2

Handling errors

Everything that can go wrong while talking to the API raises a subclass of quantumrandom.QuantumRandomError:

  • APIKeyError if no API key was found, or the API rejected it.

  • QuotaExceededError if your API key has used up its requests.

  • RateLimitError if a request was still being rate limited after being retried.

  • QuantumRandomError itself for anything else, such as the API being unreachable, timing out (after quantumrandom.TIMEOUT seconds, 30 by default, or the timeout that you passed in), or returning something unexpected.

Invalid arguments raise a ValueError without making a request.

What happened to /dev/qrandom?

Earlier versions came with qrandom-dev, which created a /dev/qrandom character device that could be used to feed the Linux entropy pool. It was removed in version 2.0. The library that it was built upon (cusepy) only ever supported Python 2 and is no longer available, and a device that makes API requests in the background for as long as something reads from it is a poor fit for an API that is metered by the request. If all you need is a stream of bytes, qrandom --binary can be piped into whatever needs it.

Development

pip install -e .[test,numpy]
pytest

The tests do not use the network or need an API key. One additional test makes a single request to the real API, and only runs if you ask it to:

QUANTUMRANDOM_LIVE_TEST=1 QUANTUMRANDOM_API_KEY=your-api-key pytest tests/test_live.py

Releasing

  1. Update VERSION in quantumrandom/__init__.py, and add the release to CHANGELOG.rst.

  2. Commit, tag, and push the release. Pushing a tag that starts with a digit runs .github/workflows/publish.yml, which builds the distributions and publishes them to PyPI:

    git tag -a 2.0.0 -m "quantumrandom 2.0.0"
    git push origin develop 2.0.0

Publishing relies on PyPI’s trusted publishing, which has to be set up once. On PyPI, add a trusted publisher for the project with owner lmacken, repository quantumrandom, workflow publish.yml, and environment pypi.

To rehearse a release on TestPyPI first, which is optional, add the same publisher there with environment testpypi, and then run the Publish workflow manually.

Metadata

Release files for quantumrandom 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for quantumrandom 2.0.0
File Size Uploaded Provenance Actions
quantumrandom-2.0.0.tar.gz 22.0 kB With provenance Details for quantumrandom-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for quantumrandom 2.0.0
File Interpreter ABI Platform Actions
quantumrandom-2.0.0-py3-none-any.whl Python 3 none any Details for quantumrandom-2.0.0-py3-none-any.whl

Total release size: 35.5 kB

Release files / quantumrandom-2.0.0.tar.gz

Download URL quantumrandom-2.0.0.tar.gz
Size 22.0 kB
Tags Source
SHA-256 checksum
How to use checksums
bfb5ff1dcc9f6fad6a0dd648681e915642149aa1230d8a9a3d324437507bf50a
BLAKE2b-256 checksum
How to use checksums
d8c023a9c309a52c979755cdc0fdd44592156843b7dc6348669837c05ae7a68d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / quantumrandom-2.0.0-py3-none-any.whl

Download URL quantumrandom-2.0.0-py3-none-any.whl
Size 13.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
89df9b3a82d8dd0a85bdef3ef62cf637460ada9f64db2dcf5a804702a2a66fcf
BLAKE2b-256 checksum
How to use checksums
1679977782d1b7a73baba049be63cefee113f014d8029abcfd3b04ad272e20b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Security for quantumrandom 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for quantumrandom-2.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page