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.6.tar.gz
(19.1 kB
view hashes)
Built Distributions
Close
Hashes for smallperm-0.1.6-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94f35850fd567c281d2905a9bfc5df87206e5ed55eba0300d653b10360bbc478 |
|
MD5 | 066d771df8e595820e1e646522f166ef |
|
BLAKE2b-256 | 5fbf94d9c23aa323ac74600d05c63c6fc518643bbc81592ccde6b0dc6bcf3f1a |
Close
Hashes for smallperm-0.1.6-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0949da61d8cc2a27c5accd3e6cccb911d259dd89c056ac3ca8f61e27822208aa |
|
MD5 | a72f2b6c170995acacd10827b71bbd98 |
|
BLAKE2b-256 | bd93558b5708d95d88a92313a42a7b5169b5af71e9a10d6287ed32ac92df70c6 |
Close
Hashes for smallperm-0.1.6-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d231a89cd2a3f9d4869549bc3ed4c31f30305869ecaa3fb3a17bc58105ed1d85 |
|
MD5 | b42968e690335dc4e90344070775f72d |
|
BLAKE2b-256 | 98cb5479ba60728bbf18d3ae3bf85a4da34c178e07bb1e3f358432c79e34fc10 |