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.2.tar.gz
(17.7 kB
view hashes)
Built Distributions
Close
Hashes for smallperm-0.1.2-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d885478f3c8692f27e9d062e15461fddc966755c2567b3cf6892e7d401797fd |
|
MD5 | 22db9a38af9f380217982d6c37fc3217 |
|
BLAKE2b-256 | 455f99e43232f91ead1e84aa27885fbb2e2e2d2309b925823b136b7dfe97ba4d |
Close
Hashes for smallperm-0.1.2-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | af9049049f769619a3c05701ef6dbe6851b1e70d221b98adec10fab8f9ab4889 |
|
MD5 | 4e1f1a667608fe19f8fb2f98375acc67 |
|
BLAKE2b-256 | 43ab30a198c517e112a3bfef0076132fe502c2bd64aa87d88484c1928bac94cd |
Close
Hashes for smallperm-0.1.2-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d4b03b5a40b9f468f3e7ec5031e77c530662f04c9bf9a4c3266e8a483b99bfc |
|
MD5 | 7b6d50f08983af8b55d46ff341450dce |
|
BLAKE2b-256 | 72918bbd528894aaee4a6d7afa2e176525f159a7184a6c143314fafede834c7c |