A simple counter-based pseudo random number generator implementation based on arXiv:2004.06278
Project description
Squares: A Counter-Based Random Number Generator
This library is an implementation of arXiv:2004.06278 adapted for any number of bits. Keep in mind that the original implementation was designed for 64 bit numbers while this implementation uses a default of 32.
The generation algorithm is NOT MY WORK. For the paper on which this utility is based, please look here.
Usage
The package can be installed with
pip install squares-rng
After installing, the generator can be used with
from squares import squares
rng = squares()
next(rng) # Returns 32-bit uint
The generator can be seeded
rng = squares(seed=42)
next(rng) # 4161798144
If you are struggling to decide on a seed to use for reproducible testing, one can be generated from the computer's internal clock
from squares import get_suitable_seed
seed = get_suitable_seed()
This function is used in the case that no seed is provided to the generator.
get_suitable_seed has no bit restrictions. If a seed containing too many bits is passed to the generator, it will be truncated and a warning will be shown.
For small seeds, the safety parameter should be left as True
rng = squares(seed=42, safety=False)
next(rng) # 0
next(rng) # 115605504
safety is a proprietary method to allow for smaller keys, iterating the generator until the seed has a non-zero maximal bit. Due to the nature of the algorithm, smaller keys would otherwise result in much smaller numbers for the first few iterations. With safety=True, keep in mind that the first generation will take longer than subsequent calls.
The maximal bits of the generated number can also be set
rng = squares(seed=84, bits=64)
next(rng) # 9267630197371305984
The default is bits=32 for maximal python compatibility. Keep in mind that this does not automatically influence the internal calculation except where necessary. Some internal calculations may involve more than the specified number of bits if python allows it. However, the generated integer will not exceed the requested bits.
Lastly, a truncation utility is included
from squares import truncate
truncate(6798039809, 32) # 2503072513
The truncation utility defaults to truncating the higher bits, but can be configured to preserve them instead
truncate(698320, 16, right_shift=True) # 43645
Original Paper
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file squares-rng-1.0.0.tar.gz.
File metadata
- Download URL: squares-rng-1.0.0.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50764bc2d7e233b48592668a9d93a99ec587399db58cf16353b4e582e386c9d0
|
|
| MD5 |
2610455817236daef5f009bb60c67ed5
|
|
| BLAKE2b-256 |
b29928ea9748aae40631273b4a2027e06119656d014d3a6408a0a97f628f43b2
|
File details
Details for the file squares_rng-1.0.0-py3-none-any.whl.
File metadata
- Download URL: squares_rng-1.0.0-py3-none-any.whl
- Upload date:
- Size: 16.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13d432659fbdf675c418c90f34d4e56456f6340e270850deace22c8baebfb2c6
|
|
| MD5 |
7e313b0e189b3e0c3ec921bae35c7a16
|
|
| BLAKE2b-256 |
968a98a66d82c59d373a38e7499a2887e16b1e27176804279913192732bce7b4
|