Registry / serialization / random2

random2

JSON →
library1.0.2pypypi✓ verified 87d ago

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.

pip install random2
INSTALL
IMPORT
SIG · RANDOM2
R
random2
serializationpythonv1.0.2
Install
1.5s avg
Import
10ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.010s · 17.8MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.5s · import 0.010s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

random2
import random2
import random
Using `import random` will import the built-in Python 3 `random` module, which may have different algorithmic behavior than `random2`.
randint
from random2 import randint

This quickstart demonstrates basic usage of `random2` including seeding, generating random numbers, choices, and sampling, showcasing state preservation typical for Python 2 compatibility.

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)}")
Debug
Known issues
gotchaThe `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.
fix
For standard Python 3 randomness, always prefer the built-in `random` module (`import random`).
affects: All versions
breakingThe 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.
fix
If migrating Python 2 code, carefully test to ensure reproducibility is maintained. If new to Python 3, understand that `random2` is not a drop-in replacement for `random` without considering behavioral differences.
affects: All versions when compared to Python 3's native `random`
deprecatedThe `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.
fix
Consider re-evaluating if strict Python 2 `random` compatibility is still critical. If not, refactor your codebase to use Python 3's native `random` module or an actively maintained alternative.
affects: 1.0.2 and potentially future Python versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'random2'
The random2 library is not installed in your current Python environment.
fix
Run `pip install random2` in your terminal.
AttributeError: module 'random2' has no attribute 'SystemRandom'
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`.
fix
If 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.
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.
fix
Ensure 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.
Upgrade
Version history
1.0.2latest on PyPI · released Dec 18, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
OpenAI (training)
1
Resources
random2 — pip install random2 · libregistry