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.9.tar.gz
(20.3 kB
view hashes)
Built Distributions
Close
Hashes for smallperm-0.1.9-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eac2a4db67ed53a205c826c5a0f034935b9cfffcfcd5285fc3ffe3b98085eb48 |
|
MD5 | 055579069a55c33f50bb7671746cf60d |
|
BLAKE2b-256 | 615afbb77cbe5cfbd4817e216eedec3131d1bef079c31b3ed96bb654cb46f0cf |
Close
Hashes for smallperm-0.1.9-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13d65ef6b40e8b8ad049f0e499634d0b963b2c3513fb010ea4c3865bc0620116 |
|
MD5 | e3a5e5f364207966739761b109f564ca |
|
BLAKE2b-256 | 9e93ddb4979ee3c14b601fabc829d90d85c63b131ffad0e1a33aaaf604a7a26a |
Close
Hashes for smallperm-0.1.9-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 90f96c5307c6b4878f33a4f6fcd09645e4f078f9e03a5ce26684167c866cd9b0 |
|
MD5 | 8db822fa292c4080e3602f16f9245027 |
|
BLAKE2b-256 | 086adb3875238c59e56cb2285e8568b56e0e3ed0c33e7bdfa3663c7f0e21fd62 |