Efficient O(1)-space pseudorandom permutation for large scale shuffling
Project description
smallperm
Small library to generate permutations of a list of elements using pseudo-random permutations (PRP). Uses O(1)
memory and O(1)
time to generate the next element of the permutation.
>>> from smallperm import PseudoRandomPermutation
>>> list(PseudoRandomPermutation(42, 0xDEADBEEF))
[14, 32, 25, 16, 0, 12, 5, 37, 30, 7, 40, 17, 27, 35, 21, 15, 1, 13, 38, 4, 9, 36, 20, 22, 24, 39, 41, 19, 3, 18, 8, 2, 29, 31, 6, 34, 11, 23, 26, 10, 28, 33]
API
-
Initialization:
PseudoRandomPermutation(length: int, seed: int)
- Generates a permutation of
[0, length)
usingseed
.
- Generates a permutation of
-
Usage: Iterate over the instance to get the next element of the permutation.
- Example:
list(PseudoRandomPermutation(42, 0xDEADBEEF))
- Example:
-
O(1) forward/backward mapping:
forward(i: int) -> int
: Returns thei
-th element of the permutation.backward(el: int) -> int
: Returns the index ofel
in the permutation.
Acknowledgements
Gratefully reuses code from https://github.com/asimihsan/permutation-iterator-rs which does most of the heavy lifting. Because the heavy lifting is done in Rust, this library is very efficient.
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
smallperm-0.1.5.tar.gz
(19.1 kB
view hashes)
Built Distributions
Close
Hashes for smallperm-0.1.5-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 276e836a0e4976cfc288dfedd9720b48a6c528cb960d7d9244c5d00aaecc0be6 |
|
MD5 | e26bbb4561789a00df29e326304ba066 |
|
BLAKE2b-256 | 19f821942425133ab8e41a15c38d2c07d8f9e9c32079ca38d24d2847c33fbf96 |
Close
Hashes for smallperm-0.1.5-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9be60bd4715cb69f8669a4e9bf38d9bd18d09a817e84fe3d3f7160c73bb6cb46 |
|
MD5 | e2a0dcaaccc30c9d0c020d22ba72bbe6 |
|
BLAKE2b-256 | 74b711f6da47a889335b08d619ac7637f2aa42b0fb3fb42458aa2b78cec10055 |
Close
Hashes for smallperm-0.1.5-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b36744c3fb3e440c081c7381184c0201e42a5e371405d8ef228b9669b0741ef1 |
|
MD5 | 29063547fb3fcaf34e8c727713fae37b |
|
BLAKE2b-256 | 335ff2d30903fb358aa11cb3bcba3654cd3aa1e52fe497edfca60de4828489df |