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.4.tar.gz
(17.7 kB
view hashes)
Built Distributions
Close
Hashes for smallperm-0.1.4-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 085182587c5a98c127e3e781d2b81f8a2e416eff69929acda0cbc737858a473e |
|
MD5 | a39f62460c0f34f2b82bf364287c5d0c |
|
BLAKE2b-256 | 1d33432a988b6647394d7c6dc37c6e1799f523f2c49b137a820ea111960eee14 |
Close
Hashes for smallperm-0.1.4-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e12d5c8ac38a89d7d92d2fdb5e1e8950695b3c8a0368f6882830f0f4c753c98d |
|
MD5 | 340d21b6037c0064386ab6b2c8983872 |
|
BLAKE2b-256 | aba76578f0fc7d952a9d97853313c4145e052ce7a907a1ba758b2345f70c3ddd |
Close
Hashes for smallperm-0.1.4-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48a0352896b4e16a8e703554b1aef7450e4e048711ffbea7ed6c9cfbdd128417 |
|
MD5 | 116504151a1ee00438f33f2abcde623a |
|
BLAKE2b-256 | e66bd93940267ab2fc1ee33929fb1e547c202d5bbef6e760ee7513a8dc0c6a65 |