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.3.tar.gz
(17.7 kB
view hashes)
Built Distributions
Close
Hashes for smallperm-0.1.3-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d48508acd07aef9146bb956486c6cc29eabcf5de87de9452a676899868b0a88 |
|
MD5 | 5d8e30c5a10bc713118932b6088a1bc4 |
|
BLAKE2b-256 | 90e894c0e9f5e4a3d42c31ad04108bfd959a3fd04a7d788c3f37aacaa55fdc59 |
Close
Hashes for smallperm-0.1.3-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9564d91a82ea87ce965542d256c43b4ad0f15af9969b3123834e7667aa22c03c |
|
MD5 | d14f0ee1a03dfadc756f2a706b9e0fc2 |
|
BLAKE2b-256 | 3d9ac708f239956fee7a6c4e8e3c71f4babc0fa478e923b077f07acf71bc6864 |
Close
Hashes for smallperm-0.1.3-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3eeb50e02392c8559d2b409400fe70f4b8d1ef49c21d92a391bd6f8754b70fc6 |
|
MD5 | e6ba374142742b933319cce8c59c8477 |
|
BLAKE2b-256 | d22f721c34c60bbe2212f27fcfd51ff796aa88da836b481a1227a44370823534 |