quantumrandom 1.8__tar.gz → 2.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,74 @@
1
+ Changelog
2
+ =========
3
+
4
+ 2.0.0 (2026-11-24)
5
+ ------------------
6
+
7
+ The ``qrng.anu.edu.au`` service that every earlier release talked to has been
8
+ shut down, which means that those releases no longer work. This release moves
9
+ to its replacement, `ANU Quantum Numbers <https://quantumnumbers.anu.edu.au>`_.
10
+
11
+ Things to know when upgrading
12
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13
+
14
+ * **An API key is now required.** Create one at
15
+ https://quantumnumbers.anu.edu.au and provide it with the
16
+ ``QUANTUMRANDOM_API_KEY`` environment variable, ``set_api_key()``, or the
17
+ new ``api_key`` argument. Requests are metered: the free plan currently
18
+ allows 100 a month.
19
+ * **Python 3.9 or newer is required.** Support for Python 2 is gone, and
20
+ ``six`` is no longer a dependency.
21
+ * **randint(min, max) can now return max**, to match ``random.randint``.
22
+ Before, ``randint(0, 1)`` could only ever return 0.
23
+ (`#25 <https://github.com/lmacken/quantumrandom/pull/25>`_ by
24
+ Brian Ó Donnell)
25
+ * ``randint`` returns an ``int`` again. On Python 3 it was returning a
26
+ ``float``, which also made its results very slightly uneven once rounded
27
+ down.
28
+ (`#16 <https://github.com/lmacken/quantumrandom/issues/16>`_,
29
+ `#20 <https://github.com/lmacken/quantumrandom/issues/20>`_,
30
+ `#24 <https://github.com/lmacken/quantumrandom/pull/24>`_ by
31
+ Brian Ó Donnell)
32
+ * ``block_size`` can be no larger than 10, down from 1024. This is a limit of
33
+ the new API, as is ``block_size`` now counting 16 bit values rather than
34
+ bytes for ``hex16``. ``binary()`` and ``hex()`` now default to the most that
35
+ one request can return: 20480 bytes, up from 10000.
36
+ * Invalid arguments raise ``ValueError``, and problems talking to the API
37
+ raise ``QuantumRandomError`` or one of its subclasses. Before, these were a
38
+ mix of ``Exception``, ``AssertionError`` and ``urllib`` errors.
39
+ * ``qrandom`` outputs one block and then exits, rather than making API
40
+ requests until it is killed. Pass ``--count 0`` for the old behaviour.
41
+ * ``qrandom -h`` shows help, as you would expect, rather than being short for
42
+ ``--hex``, which is now ``-x``.
43
+ * ``qrandom-dev`` and its ``/dev/qrandom`` device have been removed. The
44
+ library that they depend upon is Python 2 only, and has not been
45
+ installable for years.
46
+ (`#5 <https://github.com/lmacken/quantumrandom/issues/5>`_,
47
+ `#18 <https://github.com/lmacken/quantumrandom/issues/18>`_)
48
+
49
+ New
50
+ ~~~
51
+
52
+ * ``QuantumRandom``, a subclass of ``random.Random``, brings everything in
53
+ the ``random`` module to quantum random numbers: ``choice()``,
54
+ ``shuffle()``, ``sample()``, ``uniform()``, and so on. Based on
55
+ `#22 <https://github.com/lmacken/quantumrandom/pull/22>`_ by Caleb Johnson.
56
+ * ``randint`` and ``QuantumRandom`` buffer the random data that they have not
57
+ used yet, so that thousands of numbers can come out of a single API
58
+ request. ``randint`` used to make a request every time it was called.
59
+ (`#21 <https://github.com/lmacken/quantumrandom/issues/21>`_)
60
+ * The ``uint8`` and ``hex8`` data types are supported by ``get_data``.
61
+ * Requests time out after 30 seconds, which can be changed with the new
62
+ ``timeout`` argument. They used to wait forever.
63
+ (`#22 <https://github.com/lmacken/quantumrandom/pull/22>`_,
64
+ `#33 <https://github.com/lmacken/quantumrandom/issues/33>`_)
65
+ * Requests that are rate limited are retried automatically.
66
+ * ``qrandom --count`` works with ``--int``, and there are new ``--api-key``
67
+ and ``--version`` options.
68
+
69
+ Fixed
70
+ ~~~~~
71
+
72
+ * ``qrandom --binary`` writes raw bytes on Python 3, instead of their
73
+ ``repr()``.
74
+ * ``qrandom`` reports errors, instead of silently exiting.
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2012-2026 Luke Macken
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ include CHANGELOG.rst
2
+ recursive-include tests *.py
@@ -0,0 +1,289 @@
1
+ Metadata-Version: 2.4
2
+ Name: quantumrandom
3
+ Version: 2.0.0
4
+ Summary: A Python interface to the ANU Quantum Random Numbers Server
5
+ Author-email: Luke Macken <luke@phorex.org>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/lmacken/quantumrandom
8
+ Project-URL: Issues, https://github.com/lmacken/quantumrandom/issues
9
+ Project-URL: Changelog, https://github.com/lmacken/quantumrandom/blob/develop/CHANGELOG.rst
10
+ Keywords: quantum,random,qrng,anu
11
+ Classifier: Development Status :: 5 - Production/Stable
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Programming Language :: Python
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Classifier: Programming Language :: Python :: 3.14
23
+ Classifier: Programming Language :: Python :: 3.15
24
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
25
+ Requires-Python: >=3.9
26
+ Description-Content-Type: text/x-rst
27
+ License-File: LICENSE
28
+ Provides-Extra: numpy
29
+ Requires-Dist: numpy; extra == "numpy"
30
+ Provides-Extra: test
31
+ Requires-Dist: pytest; extra == "test"
32
+ Dynamic: license-file
33
+
34
+ Tools for utilizing the ANU Quantum Random Numbers Server
35
+ =========================================================
36
+
37
+ .. image:: https://github.com/lmacken/quantumrandom/actions/workflows/tests.yml/badge.svg?branch=develop
38
+ :target: https://github.com/lmacken/quantumrandom/actions/workflows/tests.yml
39
+ .. image:: https://img.shields.io/pypi/v/quantumrandom.svg
40
+ :target: https://pypi.org/project/quantumrandom/
41
+ .. image:: https://img.shields.io/pypi/dm/quantumrandom.svg
42
+ :target: https://pypi.org/project/quantumrandom/
43
+
44
+ This project provides tools for interacting with The ANU Quantum Random
45
+ Number Generator (`quantumnumbers.anu.edu.au
46
+ <https://quantumnumbers.anu.edu.au>`_), which generates true random numbers in
47
+ real time by measuring the quantum fluctuations of the vacuum. It communicates
48
+ with their JSON API and provides a ``qrandom`` command-line tool and a Python
49
+ API, including a drop-in replacement for Python's own ``random.Random``.
50
+
51
+ quantumrandom works on Python 3.9 and newer, and has no dependencies.
52
+
53
+ .. note:: As of version 2.0, quantumrandom uses ANU's new API, which requires
54
+ an API key. The ``qrng.anu.edu.au`` service that earlier versions talked to
55
+ has been shut down, so those versions no longer work. There are a few
56
+ other changes to be aware of when upgrading, which are listed in the
57
+ `changelog
58
+ <https://github.com/lmacken/quantumrandom/blob/develop/CHANGELOG.rst>`_.
59
+
60
+ Installing
61
+ ----------
62
+
63
+ ::
64
+
65
+ pip install quantumrandom
66
+
67
+ Getting an API key
68
+ ------------------
69
+
70
+ Sign up at `quantumnumbers.anu.edu.au <https://quantumnumbers.anu.edu.au>`_ to
71
+ create an API key. At the time of writing the free plan allows 100 requests
72
+ per month, at up to 1 request per second. A paid plan, billed per request
73
+ through the AWS Marketplace, has no monthly limit and allows 100 requests per
74
+ second. See their `pricing <https://quantumnumbers.anu.edu.au/pricing>`_ for
75
+ the details. quantumrandom works the same way with a key from any plan.
76
+
77
+ The easiest way to provide your key is with an environment variable, which
78
+ both the Python API and the ``qrandom`` command will pick up::
79
+
80
+ export QUANTUMRANDOM_API_KEY=your-api-key
81
+
82
+ You can also set it from Python, which takes precedence over the environment::
83
+
84
+ >>> import quantumrandom
85
+ >>> quantumrandom.set_api_key('your-api-key')
86
+
87
+ Lastly, anything that makes API requests accepts an ``api_key`` argument, for
88
+ when you need to use more than one key.
89
+
90
+ Making the most of your requests
91
+ --------------------------------
92
+
93
+ ANU counts requests, not the amount of data that they return, and a single
94
+ request can return anything up to 20480 bytes. quantumrandom is designed
95
+ around this: it asks for as much as it can at a time, and keeps what you have
96
+ not used yet in memory so that it does not need to ask again until it runs
97
+ out. For example, rolling a die a thousand times with
98
+ ``quantumrandom.randint(1, 6)`` costs one request, not a thousand.
99
+
100
+ Random data is only ever handed out once, and the buffered data lasts only as
101
+ long as your Python process does.
102
+
103
+ If a request is rate limited it is retried automatically, after a short
104
+ pause, up to ``quantumrandom.MAX_RETRIES`` (3) times.
105
+
106
+ Command-line tool
107
+ -----------------
108
+
109
+ ::
110
+
111
+ $ qrandom --int --min 5 --max 15
112
+ 7
113
+ $ qrandom --int --min 1 --max 6 --count 5
114
+ 5
115
+ 6
116
+ 4
117
+ 4
118
+ 1
119
+ $ qrandom --hex
120
+ 1dc59fde43b5045120453186d45653dd455bd8e6fc7d8c591f0018fa9261ab2835eb210e8
121
+ e267cf35a54c02ce2a93b3ec448c4c7aa84fdedb61c7b0d87c9e7acf8e9fdadc8d68bcaa5a
122
+ ...
123
+ $ qrandom --binary > data
124
+ $ ls -l data
125
+ -rw-r--r-- 1 user user 20480 Nov 24 12:00 data
126
+
127
+ ``--hex`` and ``--binary`` output one block of 20480 bytes, at the cost of one
128
+ API request. Use ``--count`` to ask for more than one block (or more than one
129
+ integer), or ``--count 0`` to keep going until interrupted::
130
+
131
+ $ qrandom --binary --count 0 | dd of=data
132
+
133
+ .. warning:: ``--count 0`` makes one API request after another for as long as
134
+ it runs. On the free plan that will use up your month's requests in a
135
+ couple of minutes.
136
+
137
+ Run ``qrandom --help`` for the full list of options.
138
+
139
+ Python API
140
+ ----------
141
+
142
+ The simplest way to get going is with ``quantumrandom.QuantumRandom``, which
143
+ is a subclass of `random.Random
144
+ <https://docs.python.org/3/library/random.html>`_ that gets its randomness
145
+ from the quantum random number generator. It supports everything that the
146
+ ``random`` module does.
147
+
148
+ ::
149
+
150
+ >>> import quantumrandom
151
+ >>> qr = quantumrandom.QuantumRandom()
152
+ >>> qr.random()
153
+ 0.794368699495203
154
+ >>> qr.randint(1, 6)
155
+ 4
156
+ >>> qr.choice(['heads', 'tails'])
157
+ 'tails'
158
+ >>> qr.sample(range(1, 50), 6)
159
+ [12, 49, 36, 43, 31, 25]
160
+ >>> deck = list(range(52))
161
+ >>> qr.shuffle(deck)
162
+ >>> qr.randbytes(16).hex()
163
+ 'e686697890ac68804bd8075067edd33d'
164
+
165
+ Just like ``random.SystemRandom``, it has no state to seed or save, so
166
+ sequences cannot be reproduced. Each instance keeps its own buffer of random
167
+ data, and can be given its own ``api_key``.
168
+
169
+ There is also a ``randint`` function at the top level, for convenience. It
170
+ draws from a buffer that is shared by the whole process.
171
+
172
+ ::
173
+
174
+ >>> quantumrandom.randint(0, 20)
175
+ 5
176
+
177
+ .. note:: As of version 2.0, ``randint(min, max)`` can return ``max``, just
178
+ like ``random.randint`` does. Earlier versions never did.
179
+
180
+ The low-level ``get_data`` function is modelled after the ANU Quantum Random
181
+ Number Generator's JSON API. It makes one request, and returns a list of
182
+ ``uint8`` or ``uint16`` integers, or ``hex8`` or ``hex16`` strings.
183
+
184
+ ::
185
+
186
+ >>> quantumrandom.get_data()
187
+ [26646]
188
+ >>> quantumrandom.get_data(data_type='uint16', array_length=5)
189
+ [22546, 58383, 55100, 9936, 55159]
190
+ >>> quantumrandom.get_data(data_type='hex8', array_length=3, block_size=2)
191
+ ['8350', 'df68', 'ae1f']
192
+ >>> quantumrandom.get_data(data_type='hex16', array_length=2, block_size=3)
193
+ ['8ba57d8c7d1e', 'bc54a2eb5a07']
194
+
195
+ The ``array_length`` cannot be larger than ``1024``. The ``block_size`` only
196
+ applies to the hex data types, where it is the number of 8 or 16 bit values
197
+ in each string, and cannot be larger than ``10``.
198
+
199
+ Based on this ``get_data`` function, quantumrandom also provides a few
200
+ higher-level helper functions. Each of these makes one request when called.
201
+
202
+ ::
203
+
204
+ >>> quantumrandom.hex()[:10]
205
+ '8272613343'
206
+ >>> len(quantumrandom.binary())
207
+ 20480
208
+ >>> quantumrandom.binary(array_length=8, block_size=1)
209
+ b'\x07kE\xbb\x7f\xf8\x12\x83\xd5\xc6\xa2\xd17\x8e\x03\x17'
210
+ >>> quantumrandom.uint16()
211
+ array([24094, 13944, 22109, 22908, 34878, 33797, 47221, 21485, 37930, ...], dtype=uint16)
212
+
213
+ ``uint16`` requires `NumPy <https://numpy.org>`_, which you can install along
214
+ with quantumrandom by running ``pip install quantumrandom[numpy]``.
215
+
216
+ ``cached_generator`` returns a generator that yields numbers forever, making
217
+ a request for 1024 more each time it runs out. One can be passed to
218
+ ``randint`` to have it take its numbers from there instead.
219
+
220
+ ::
221
+
222
+ >>> generator = quantumrandom.cached_generator()
223
+ >>> next(generator)
224
+ 48013
225
+ >>> quantumrandom.randint(1, 6, generator)
226
+ 2
227
+
228
+ Handling errors
229
+ ---------------
230
+
231
+ Everything that can go wrong while talking to the API raises a subclass of
232
+ ``quantumrandom.QuantumRandomError``:
233
+
234
+ * ``APIKeyError`` if no API key was found, or the API rejected it.
235
+ * ``QuotaExceededError`` if your API key has used up its requests.
236
+ * ``RateLimitError`` if a request was still being rate limited after being
237
+ retried.
238
+ * ``QuantumRandomError`` itself for anything else, such as the API being
239
+ unreachable, timing out (after ``quantumrandom.TIMEOUT`` seconds, 30 by
240
+ default, or the ``timeout`` that you passed in), or returning something
241
+ unexpected.
242
+
243
+ Invalid arguments raise a ``ValueError`` without making a request.
244
+
245
+ What happened to /dev/qrandom?
246
+ ------------------------------
247
+
248
+ Earlier versions came with ``qrandom-dev``, which created a ``/dev/qrandom``
249
+ character device that could be used to feed the Linux entropy pool. It was
250
+ removed in version 2.0. The library that it was built upon (cusepy) only ever
251
+ supported Python 2 and is no longer available, and a device that makes API
252
+ requests in the background for as long as something reads from it is a poor
253
+ fit for an API that is metered by the request. If all you need is a stream of
254
+ bytes, ``qrandom --binary`` can be piped into whatever needs it.
255
+
256
+ Development
257
+ -----------
258
+
259
+ ::
260
+
261
+ pip install -e .[test,numpy]
262
+ pytest
263
+
264
+ The tests do not use the network or need an API key. One additional test
265
+ makes a single request to the real API, and only runs if you ask it to::
266
+
267
+ QUANTUMRANDOM_LIVE_TEST=1 QUANTUMRANDOM_API_KEY=your-api-key pytest tests/test_live.py
268
+
269
+ Releasing
270
+ ---------
271
+
272
+ 1. Update ``VERSION`` in ``quantumrandom/__init__.py``, and add the release to
273
+ ``CHANGELOG.rst``.
274
+ 2. Commit, tag, and push the release. Pushing a tag that starts with a digit
275
+ runs ``.github/workflows/publish.yml``, which builds the distributions and
276
+ publishes them to PyPI::
277
+
278
+ git tag -a 2.0.0 -m "quantumrandom 2.0.0"
279
+ git push origin develop 2.0.0
280
+
281
+ Publishing relies on PyPI's `trusted publishing
282
+ <https://docs.pypi.org/trusted-publishers/>`_, which has to be set up once. On
283
+ PyPI, add a trusted publisher for the project with owner ``lmacken``,
284
+ repository ``quantumrandom``, workflow ``publish.yml``, and environment
285
+ ``pypi``.
286
+
287
+ To rehearse a release on TestPyPI first, which is optional, add the same
288
+ publisher there with environment ``testpypi``, and then run the **Publish**
289
+ workflow manually.
@@ -0,0 +1,256 @@
1
+ Tools for utilizing the ANU Quantum Random Numbers Server
2
+ =========================================================
3
+
4
+ .. image:: https://github.com/lmacken/quantumrandom/actions/workflows/tests.yml/badge.svg?branch=develop
5
+ :target: https://github.com/lmacken/quantumrandom/actions/workflows/tests.yml
6
+ .. image:: https://img.shields.io/pypi/v/quantumrandom.svg
7
+ :target: https://pypi.org/project/quantumrandom/
8
+ .. image:: https://img.shields.io/pypi/dm/quantumrandom.svg
9
+ :target: https://pypi.org/project/quantumrandom/
10
+
11
+ This project provides tools for interacting with The ANU Quantum Random
12
+ Number Generator (`quantumnumbers.anu.edu.au
13
+ <https://quantumnumbers.anu.edu.au>`_), which generates true random numbers in
14
+ real time by measuring the quantum fluctuations of the vacuum. It communicates
15
+ with their JSON API and provides a ``qrandom`` command-line tool and a Python
16
+ API, including a drop-in replacement for Python's own ``random.Random``.
17
+
18
+ quantumrandom works on Python 3.9 and newer, and has no dependencies.
19
+
20
+ .. note:: As of version 2.0, quantumrandom uses ANU's new API, which requires
21
+ an API key. The ``qrng.anu.edu.au`` service that earlier versions talked to
22
+ has been shut down, so those versions no longer work. There are a few
23
+ other changes to be aware of when upgrading, which are listed in the
24
+ `changelog
25
+ <https://github.com/lmacken/quantumrandom/blob/develop/CHANGELOG.rst>`_.
26
+
27
+ Installing
28
+ ----------
29
+
30
+ ::
31
+
32
+ pip install quantumrandom
33
+
34
+ Getting an API key
35
+ ------------------
36
+
37
+ Sign up at `quantumnumbers.anu.edu.au <https://quantumnumbers.anu.edu.au>`_ to
38
+ create an API key. At the time of writing the free plan allows 100 requests
39
+ per month, at up to 1 request per second. A paid plan, billed per request
40
+ through the AWS Marketplace, has no monthly limit and allows 100 requests per
41
+ second. See their `pricing <https://quantumnumbers.anu.edu.au/pricing>`_ for
42
+ the details. quantumrandom works the same way with a key from any plan.
43
+
44
+ The easiest way to provide your key is with an environment variable, which
45
+ both the Python API and the ``qrandom`` command will pick up::
46
+
47
+ export QUANTUMRANDOM_API_KEY=your-api-key
48
+
49
+ You can also set it from Python, which takes precedence over the environment::
50
+
51
+ >>> import quantumrandom
52
+ >>> quantumrandom.set_api_key('your-api-key')
53
+
54
+ Lastly, anything that makes API requests accepts an ``api_key`` argument, for
55
+ when you need to use more than one key.
56
+
57
+ Making the most of your requests
58
+ --------------------------------
59
+
60
+ ANU counts requests, not the amount of data that they return, and a single
61
+ request can return anything up to 20480 bytes. quantumrandom is designed
62
+ around this: it asks for as much as it can at a time, and keeps what you have
63
+ not used yet in memory so that it does not need to ask again until it runs
64
+ out. For example, rolling a die a thousand times with
65
+ ``quantumrandom.randint(1, 6)`` costs one request, not a thousand.
66
+
67
+ Random data is only ever handed out once, and the buffered data lasts only as
68
+ long as your Python process does.
69
+
70
+ If a request is rate limited it is retried automatically, after a short
71
+ pause, up to ``quantumrandom.MAX_RETRIES`` (3) times.
72
+
73
+ Command-line tool
74
+ -----------------
75
+
76
+ ::
77
+
78
+ $ qrandom --int --min 5 --max 15
79
+ 7
80
+ $ qrandom --int --min 1 --max 6 --count 5
81
+ 5
82
+ 6
83
+ 4
84
+ 4
85
+ 1
86
+ $ qrandom --hex
87
+ 1dc59fde43b5045120453186d45653dd455bd8e6fc7d8c591f0018fa9261ab2835eb210e8
88
+ e267cf35a54c02ce2a93b3ec448c4c7aa84fdedb61c7b0d87c9e7acf8e9fdadc8d68bcaa5a
89
+ ...
90
+ $ qrandom --binary > data
91
+ $ ls -l data
92
+ -rw-r--r-- 1 user user 20480 Nov 24 12:00 data
93
+
94
+ ``--hex`` and ``--binary`` output one block of 20480 bytes, at the cost of one
95
+ API request. Use ``--count`` to ask for more than one block (or more than one
96
+ integer), or ``--count 0`` to keep going until interrupted::
97
+
98
+ $ qrandom --binary --count 0 | dd of=data
99
+
100
+ .. warning:: ``--count 0`` makes one API request after another for as long as
101
+ it runs. On the free plan that will use up your month's requests in a
102
+ couple of minutes.
103
+
104
+ Run ``qrandom --help`` for the full list of options.
105
+
106
+ Python API
107
+ ----------
108
+
109
+ The simplest way to get going is with ``quantumrandom.QuantumRandom``, which
110
+ is a subclass of `random.Random
111
+ <https://docs.python.org/3/library/random.html>`_ that gets its randomness
112
+ from the quantum random number generator. It supports everything that the
113
+ ``random`` module does.
114
+
115
+ ::
116
+
117
+ >>> import quantumrandom
118
+ >>> qr = quantumrandom.QuantumRandom()
119
+ >>> qr.random()
120
+ 0.794368699495203
121
+ >>> qr.randint(1, 6)
122
+ 4
123
+ >>> qr.choice(['heads', 'tails'])
124
+ 'tails'
125
+ >>> qr.sample(range(1, 50), 6)
126
+ [12, 49, 36, 43, 31, 25]
127
+ >>> deck = list(range(52))
128
+ >>> qr.shuffle(deck)
129
+ >>> qr.randbytes(16).hex()
130
+ 'e686697890ac68804bd8075067edd33d'
131
+
132
+ Just like ``random.SystemRandom``, it has no state to seed or save, so
133
+ sequences cannot be reproduced. Each instance keeps its own buffer of random
134
+ data, and can be given its own ``api_key``.
135
+
136
+ There is also a ``randint`` function at the top level, for convenience. It
137
+ draws from a buffer that is shared by the whole process.
138
+
139
+ ::
140
+
141
+ >>> quantumrandom.randint(0, 20)
142
+ 5
143
+
144
+ .. note:: As of version 2.0, ``randint(min, max)`` can return ``max``, just
145
+ like ``random.randint`` does. Earlier versions never did.
146
+
147
+ The low-level ``get_data`` function is modelled after the ANU Quantum Random
148
+ Number Generator's JSON API. It makes one request, and returns a list of
149
+ ``uint8`` or ``uint16`` integers, or ``hex8`` or ``hex16`` strings.
150
+
151
+ ::
152
+
153
+ >>> quantumrandom.get_data()
154
+ [26646]
155
+ >>> quantumrandom.get_data(data_type='uint16', array_length=5)
156
+ [22546, 58383, 55100, 9936, 55159]
157
+ >>> quantumrandom.get_data(data_type='hex8', array_length=3, block_size=2)
158
+ ['8350', 'df68', 'ae1f']
159
+ >>> quantumrandom.get_data(data_type='hex16', array_length=2, block_size=3)
160
+ ['8ba57d8c7d1e', 'bc54a2eb5a07']
161
+
162
+ The ``array_length`` cannot be larger than ``1024``. The ``block_size`` only
163
+ applies to the hex data types, where it is the number of 8 or 16 bit values
164
+ in each string, and cannot be larger than ``10``.
165
+
166
+ Based on this ``get_data`` function, quantumrandom also provides a few
167
+ higher-level helper functions. Each of these makes one request when called.
168
+
169
+ ::
170
+
171
+ >>> quantumrandom.hex()[:10]
172
+ '8272613343'
173
+ >>> len(quantumrandom.binary())
174
+ 20480
175
+ >>> quantumrandom.binary(array_length=8, block_size=1)
176
+ b'\x07kE\xbb\x7f\xf8\x12\x83\xd5\xc6\xa2\xd17\x8e\x03\x17'
177
+ >>> quantumrandom.uint16()
178
+ array([24094, 13944, 22109, 22908, 34878, 33797, 47221, 21485, 37930, ...], dtype=uint16)
179
+
180
+ ``uint16`` requires `NumPy <https://numpy.org>`_, which you can install along
181
+ with quantumrandom by running ``pip install quantumrandom[numpy]``.
182
+
183
+ ``cached_generator`` returns a generator that yields numbers forever, making
184
+ a request for 1024 more each time it runs out. One can be passed to
185
+ ``randint`` to have it take its numbers from there instead.
186
+
187
+ ::
188
+
189
+ >>> generator = quantumrandom.cached_generator()
190
+ >>> next(generator)
191
+ 48013
192
+ >>> quantumrandom.randint(1, 6, generator)
193
+ 2
194
+
195
+ Handling errors
196
+ ---------------
197
+
198
+ Everything that can go wrong while talking to the API raises a subclass of
199
+ ``quantumrandom.QuantumRandomError``:
200
+
201
+ * ``APIKeyError`` if no API key was found, or the API rejected it.
202
+ * ``QuotaExceededError`` if your API key has used up its requests.
203
+ * ``RateLimitError`` if a request was still being rate limited after being
204
+ retried.
205
+ * ``QuantumRandomError`` itself for anything else, such as the API being
206
+ unreachable, timing out (after ``quantumrandom.TIMEOUT`` seconds, 30 by
207
+ default, or the ``timeout`` that you passed in), or returning something
208
+ unexpected.
209
+
210
+ Invalid arguments raise a ``ValueError`` without making a request.
211
+
212
+ What happened to /dev/qrandom?
213
+ ------------------------------
214
+
215
+ Earlier versions came with ``qrandom-dev``, which created a ``/dev/qrandom``
216
+ character device that could be used to feed the Linux entropy pool. It was
217
+ removed in version 2.0. The library that it was built upon (cusepy) only ever
218
+ supported Python 2 and is no longer available, and a device that makes API
219
+ requests in the background for as long as something reads from it is a poor
220
+ fit for an API that is metered by the request. If all you need is a stream of
221
+ bytes, ``qrandom --binary`` can be piped into whatever needs it.
222
+
223
+ Development
224
+ -----------
225
+
226
+ ::
227
+
228
+ pip install -e .[test,numpy]
229
+ pytest
230
+
231
+ The tests do not use the network or need an API key. One additional test
232
+ makes a single request to the real API, and only runs if you ask it to::
233
+
234
+ QUANTUMRANDOM_LIVE_TEST=1 QUANTUMRANDOM_API_KEY=your-api-key pytest tests/test_live.py
235
+
236
+ Releasing
237
+ ---------
238
+
239
+ 1. Update ``VERSION`` in ``quantumrandom/__init__.py``, and add the release to
240
+ ``CHANGELOG.rst``.
241
+ 2. Commit, tag, and push the release. Pushing a tag that starts with a digit
242
+ runs ``.github/workflows/publish.yml``, which builds the distributions and
243
+ publishes them to PyPI::
244
+
245
+ git tag -a 2.0.0 -m "quantumrandom 2.0.0"
246
+ git push origin develop 2.0.0
247
+
248
+ Publishing relies on PyPI's `trusted publishing
249
+ <https://docs.pypi.org/trusted-publishers/>`_, which has to be set up once. On
250
+ PyPI, add a trusted publisher for the project with owner ``lmacken``,
251
+ repository ``quantumrandom``, workflow ``publish.yml``, and environment
252
+ ``pypi``.
253
+
254
+ To rehearse a release on TestPyPI first, which is optional, add the same
255
+ publisher there with environment ``testpypi``, and then run the **Publish**
256
+ workflow manually.