random2 Library

1.0.2 · abandoned · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

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)}")

view raw JSON →