Skip to main content

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) using seed.
  • Usage: Iterate over the instance to get the next element of the permutation.

    • Example: list(PseudoRandomPermutation(42, 0xDEADBEEF))
  • O(1) forward/backward mapping:

    • forward(i: int) -> int: Returns the i-th element of the permutation.
    • backward(el: int) -> int: Returns the index of el 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


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)

Uploaded Source

Built Distributions

smallperm-0.1.4-cp37-abi3-win_amd64.whl (134.6 kB view hashes)

Uploaded CPython 3.7+ Windows x86-64

smallperm-0.1.4-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (267.9 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

smallperm-0.1.4-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (477.7 kB view hashes)

Uploaded CPython 3.7+ macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page