Pyudorandom
Pyudorandom is a Python library that generates pseudorandom sequences by using algebraic methods. It allows iteration over a list in a non-successive, yet deterministic way, ensuring each item is yielded exactly once. This is particularly useful when you need to mix up items without the overhead or cryptographic guarantees of true randomness, often outperforming `random.shuffle` for longer lists. The current version is 1.0.0, and releases are generally made on an as-needed basis.
Common errors
-
ImportError: cannot import name 'shuffle' from 'pyudorandom'
cause Attempting to import `shuffle` directly as if it were a top-level function in the module.fixThe `shuffle` function is a method of the `pyudorandom` module itself. You should `import pyudorandom` and then call `pyudorandom.shuffle(my_list)`. -
TypeError: object of type 'int' has no len()
cause Passing a non-iterable object (like an integer) to `pyudorandom.shuffle()` or `pyudorandom.items()`, which expect an iterable (e.g., list, tuple, string).fixEnsure you pass an iterable (e.g., a list or tuple) to `pyudorandom.shuffle()` or `pyudorandom.items()`. For example, `pyudorandom.shuffle([1, 2, 3])` works, but `pyudorandom.shuffle(123)` does not.
Warnings
- gotcha Pyudorandom generates pseudorandom numbers deterministically, meaning given the same input, it will always produce the same sequence. It is not suitable for cryptographic purposes or applications requiring true unpredictability. For security-sensitive randomness, use Python's `secrets` module.
- gotcha For very small lists, `pyudorandom` might be slower than Python's built-in `random.shuffle`. This is due to its internal mechanism which involves finding a number `m` coprime to the list's length `n`.
Install
-
pip install pyudorandom
Imports
- pyudorandom
from pyudorandom import shuffle
import pyudorandom
Quickstart
import pyudorandom
my_list = [1, 5, 7, 3, 2, 8, 4, 6]
# Get a new list with elements in a 'pseudorandom' order
shuffled_list = pyudorandom.shuffle(my_list)
print(f"Original list: {my_list}")
print(f"Shuffled list: {shuffled_list}")
# Iterate through items in a 'pseudorandom' order
print("\nIterating through items:")
for item in pyudorandom.items(my_list):
print(item)