| 1 |
.. _librt-random: |
| 2 |
|
| 3 |
librt.random |
| 4 |
============ |
| 5 |
|
| 6 |
The ``librt.random`` module is part of the ``librt`` package on PyPI, and it provides |
| 7 |
pseudorandom number generation utilities. It can be used as a significantly faster |
| 8 |
alternative to the stdlib :mod:`random` module in compiled code. It can also be faster |
| 9 |
than stdlib ``random`` in interpreted code, depending on use case. |
| 10 |
|
| 11 |
The module uses the `ChaCha8 <https://cr.yp.to/chacha.html>`__ algorithm with forward |
| 12 |
secrecy. It is **not** suitable for cryptographic use, but it provides high-quality, |
| 13 |
statistically uniform output. |
| 14 |
|
| 15 |
Functions |
| 16 |
--------- |
| 17 |
|
| 18 |
The module provides module-level functions that use thread-local state, so they are |
| 19 |
safe to call concurrently from multiple threads without external locking, and they |
| 20 |
scale well even if used from multiple threads: |
| 21 |
|
| 22 |
.. function:: random() -> float |
| 23 |
|
| 24 |
Return a random floating-point number in the range [0.0, 1.0). |
| 25 |
|
| 26 |
.. function:: randint(a: i64, b: i64) -> i64 |
| 27 |
|
| 28 |
Return a random integer *n* such that *a* <= *n* <= *b*. |
| 29 |
|
| 30 |
.. function:: randrange(stop: i64, /) -> i64 |
| 31 |
randrange(start: i64, stop: i64, /) -> i64 |
| 32 |
|
| 33 |
Return a random integer from the range. With one argument, the range is [0, *stop*). |
| 34 |
With two arguments, the range is [*start*, *stop*). |
| 35 |
|
| 36 |
.. function:: seed(n: i64, /) -> None |
| 37 |
|
| 38 |
Seed the thread-local random number generator. This only affects module-level |
| 39 |
functions called from the current thread. |
| 40 |
|
| 41 |
Random class |
| 42 |
------------ |
| 43 |
|
| 44 |
.. class:: Random(seed: i64 | None = None) |
| 45 |
|
| 46 |
A pseudorandom number generator instance with its own independent state. Use this |
| 47 |
when you need reproducible sequences or want to avoid interference with the |
| 48 |
thread-local state used by the module-level functions. |
| 49 |
|
| 50 |
If *seed* is ``None``, the generator is seeded from OS entropy |
| 51 |
(via :func:`os.urandom`). |
| 52 |
|
| 53 |
It's not safe to use the same ``Random`` instance concurrently from multiple |
| 54 |
threads without synchronization on free-threaded Python builds. |
| 55 |
|
| 56 |
.. method:: random() -> float |
| 57 |
|
| 58 |
Return a random floating-point number in the range [0.0, 1.0). |
| 59 |
|
| 60 |
.. method:: randint(a: i64, b: i64) -> i64 |
| 61 |
|
| 62 |
Return a random integer *n* such that *a* <= *n* <= *b*. |
| 63 |
|
| 64 |
.. method:: randrange(stop: i64, /) -> i64 |
| 65 |
randrange(start: i64, stop: i64, /) -> i64 |
| 66 |
|
| 67 |
Return a random integer from the range. With one argument, the range is [0, *stop*). |
| 68 |
With two arguments, the range is [*start*, *stop*). |
| 69 |
|
| 70 |
.. method:: seed(n: i64, /) -> None |
| 71 |
|
| 72 |
Reseed the generator. |
| 73 |
|
| 74 |
Example |
| 75 |
------- |
| 76 |
|
| 77 |
Using module-level functions:: |
| 78 |
|
| 79 |
from librt.random import randint, seed |
| 80 |
|
| 81 |
def roll_dice() -> i64: |
| 82 |
return randint(1, 6) |
| 83 |
|
| 84 |
Using a ``Random`` instance for reproducible sequences:: |
| 85 |
|
| 86 |
from librt.random import Random |
| 87 |
|
| 88 |
def generate_data() -> list[i64]: |
| 89 |
rng = Random(42) |
| 90 |
return [rng.randint(0, 100) for _ in range(10)] |
| 91 |
|
| 92 |
Backward compatibility |
| 93 |
---------------------- |
| 94 |
|
| 95 |
New versions of this module are not guaranteed to generate the same results when |
| 96 |
using the same seed. A specific seed only produces predictable random numbers on a |
| 97 |
specific version of ``librt``. In the future we might provide stronger guarantees. |