{"id":10188,"library":"random2","title":"random2 Library","description":"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.","status":"abandoned","version":"1.0.2","language":"en","source_language":"en","source_url":"https://github.com/fdintino/random2","tags":["randomness","compatibility","python2","python3","migration"],"install":[{"cmd":"pip install random2","lang":"bash","label":"Install random2"}],"dependencies":[],"imports":[{"note":"Using `import random` will import the built-in Python 3 `random` module, which may have different algorithmic behavior than `random2`.","wrong":"import random","symbol":"random2","correct":"import random2"},{"symbol":"randint","correct":"from random2 import randint"}],"quickstart":{"code":"import random2\n\n# Seed for reproducibility (mimics Python 2 behavior)\nrandom2.seed(42)\n\nprint(f\"Random integer between 1 and 10: {random2.randint(1, 10)}\")\nprint(f\"Random choice from a list: {random2.choice(['apple', 'banana', 'cherry'])}\")\nprint(f\"Random sample of 2 from a list: {random2.sample(range(10), 2)}\")\n\n# Demonstrates getting/setting state for exact reproduction\nstate = random2.getstate()\nprint(f\"Another random integer: {random2.randint(1, 10)}\")\nrandom2.setstate(state)\nprint(f\"Same random integer after restoring state: {random2.randint(1, 10)}\")","lang":"python","description":"This quickstart demonstrates basic usage of `random2` including seeding, generating random numbers, choices, and sampling, showcasing state preservation typical for Python 2 compatibility."},"warnings":[{"fix":"For standard Python 3 randomness, always prefer the built-in `random` module (`import random`).","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"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.","severity":"breaking","affected_versions":"All versions when compared to Python 3's native `random`"},{"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.","message":"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.","severity":"deprecated","affected_versions":"1.0.2 and potentially future Python versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Run `pip install random2` in your terminal.","cause":"The random2 library is not installed in your current Python environment.","error":"ModuleNotFoundError: No module named 'random2'"},{"fix":"If you need `SystemRandom`, you must use the built-in Python 3 `random` module: `import random; sys_random = random.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`.","error":"AttributeError: module 'random2' has no attribute 'SystemRandom'"},{"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.","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.","error":"Unexpected random sequence or non-reproducible results when migrating from Python 2 to Python 3, even with the same seed."}]}