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.7.tar.gz
(20.3 kB
view hashes)
Built Distributions
Close
Hashes for smallperm-0.1.7-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea75866bea2d5415434565009bb1ee28eba8c199529e6114693a7685e679774d |
|
MD5 | a93a275b9df1868ce5a10066b7ea6f99 |
|
BLAKE2b-256 | 6313c2c30299be9224e7cf4b9aa88fa0bb106b237328eb5a45a83c69c9b3ea8f |
Close
Hashes for smallperm-0.1.7-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aca7a095f35650c1e4798d697983e4cdd37473c5737204a77bad9ca9c0670aa8 |
|
MD5 | 9872e671c42ad0f750a27fa3ac6776ea |
|
BLAKE2b-256 | e45f4d6e2afcdf5a46bed75707ccad64f927c0a9693fee26bd9a98d44337e362 |
Close
Hashes for smallperm-0.1.7-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8cb8657b4344d4586265a696f77ec4c61b56db7d401b7395a8683c9d111575af |
|
MD5 | 41439ebeea0bc0705c14ef4ce668691e |
|
BLAKE2b-256 | 4d734806445674faf182ffa79d93b7ce3b5ec93265da40ad10e19be3913c0332 |