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.1.tar.gz
(17.7 kB
view hashes)
Built Distributions
Close
Hashes for smallperm-0.1.1-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88b3c2f14edb6e9355c3f0e28ae5edaced9f8bcd4923c9284f95cff780387e21 |
|
MD5 | 9d3192fadc4dcee1e1a59e5c72f1fe16 |
|
BLAKE2b-256 | f836211cc15c14cad4eebb3e4633789a04fe8748bd02832eadf146b4e3248882 |
Close
Hashes for smallperm-0.1.1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9dfa6deb06480ce6aa0bc138411f3927ec7e26d73b36e8b04189d13c81bb07d |
|
MD5 | 3cedddaf9c119698db18df560caeadd9 |
|
BLAKE2b-256 | 9df4aa1cdc286d6f9d88d597e0b33fc5444515235a7fa324b0bbae8584d89d04 |
Close
Hashes for smallperm-0.1.1-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba56641997958d390882586406a45cd06f16d4beca61cb4d0ba3e5cc15bce531 |
|
MD5 | a314acd7bd1d1fc40132f0f76e6655da |
|
BLAKE2b-256 | 06cba97670198aa40cb308bb5c427573d079d7bc5c8bb549051fcf0434240f25 |