random2 Library
random2 is a Python library that provides a Python 3 compatible implementation of the Python 2 `random` module. Its primary purpose is to assist in migrating Python 2 code to Python 3 by preserving the exact behavior and algorithms of the older `random` module, particularly for applications requiring reproducible results from Python 2 seeds. The current version is 1.0.2, and the library appears to be unmaintained since 2017.
Common errors
-
ModuleNotFoundError: No module named 'random2'
cause The random2 library is not installed in your current Python environment.fixRun `pip install random2` in your terminal. -
AttributeError: module 'random2' has no attribute 'SystemRandom'
cause The `random2` module is a Python 2 `random` compatibility layer for Python 3. The `SystemRandom` class, which provides access to cryptographically secure random numbers, was introduced in Python 3's native `random` module and is not part of `random2`.fixIf you need `SystemRandom`, you must use the built-in Python 3 `random` module: `import random; sys_random = random.SystemRandom()`. -
Unexpected random sequence or non-reproducible results when migrating from Python 2 to Python 3, even with the same seed.
cause This typically occurs when code written for Python 2's `random` module (which `random2` mimics) is run using Python 3's built-in `random` module, whose underlying algorithms for some functions can differ.fixEnsure you are explicitly importing and using `random2` (e.g., `import random2; random2.seed(...)`) if you require strict Python 2 `random` behavior for reproducibility in Python 3. Otherwise, adapt your tests and expectations for Python 3's native `random` module.
Warnings
- gotcha The `random2` library is designed specifically for Python 2 compatibility in Python 3. It should generally not be used for new Python 3 projects or if you don't specifically need Python 2 `random` module's exact behavior.
- breaking The algorithmic behavior of `random2` (mimicking Python 2's `random`) can differ subtly from Python 3's built-in `random` module, especially concerning seeding and specific statistical distributions. This can lead to different sequences of 'random' numbers.
- deprecated The `random2` library has not been updated since 2017 and is effectively abandoned. It may not receive security fixes, bug fixes, or compatibility updates for newer Python versions.
Install
-
pip install random2
Imports
- random2
import random
import random2
- randint
from random2 import randint
Quickstart
import random2
# Seed for reproducibility (mimics Python 2 behavior)
random2.seed(42)
print(f"Random integer between 1 and 10: {random2.randint(1, 10)}")
print(f"Random choice from a list: {random2.choice(['apple', 'banana', 'cherry'])}")
print(f"Random sample of 2 from a list: {random2.sample(range(10), 2)}")
# Demonstrates getting/setting state for exact reproduction
state = random2.getstate()
print(f"Another random integer: {random2.randint(1, 10)}")
random2.setstate(state)
print(f"Same random integer after restoring state: {random2.randint(1, 10)}")