{"id":9738,"library":"fastrand","title":"Fast Random Number Generation","description":"fastrand provides significantly faster random number generation in Python compared to the standard `random` module, implemented as a C extension. It is suitable for applications where speed is critical and cryptographic security is not required. The current version is 3.0.8, with a maintenance cadence of small patches and occasional feature additions.","status":"active","version":"3.0.8","language":"en","source_language":"en","source_url":"https://github.com/lemire/fastrand","tags":["random","performance","fast","numbers","pcg"],"install":[{"cmd":"pip install fastrand","lang":"bash","label":"Install fastrand"}],"dependencies":[],"imports":[{"note":"The fastrand module is typically imported directly; its functions are accessed via 'fastrand.function_name'.","wrong":"from fastrand import random","symbol":"fastrand","correct":"import fastrand"}],"quickstart":{"code":"import fastrand\n\nprint(\"Random integer [0, 99]:\", fastrand.pcg32bounded(100))\nprint(\"Random 32-bit unsigned int:\", fastrand.pcg32())\nprint(\"Random double [0, 1):\")\nfor _ in range(3):\n    print(f\"  {fastrand.randdouble():.6f}\")","lang":"python","description":"This example demonstrates how to import the fastrand module and use its primary functions: `pcg32bounded` for bounded integers, `pcg32` for unbounded 32-bit unsigned integers, and `randdouble` for floating-point numbers between 0 (inclusive) and 1 (exclusive)."},"warnings":[{"fix":"For cryptographically secure random numbers, use Python's `secrets` module (e.g., `secrets.randbelow`, `secrets.token_hex`) or `os.urandom()`.","message":"fastrand is NOT cryptographically secure. Do not use it for security-sensitive applications, such as generating passwords, cryptographic keys, or tokens. It uses the PCG (Permuted Congruential Generator) algorithm, which is fast but not designed for cryptographic strength.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Carefully review the `fastrand` documentation for the correct function names and signatures. Do not assume `fastrand.randint()` or `fastrand.seed()` will work as they do in the `random` module.","message":"fastrand is not a drop-in replacement for Python's built-in `random` module. Its API uses different function names (e.g., `pcg32bounded` instead of `randint`, `randdouble` instead of `random()`) and does not support seeding or state management in the same way.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Rely on fastrand's internal seeding mechanism. If explicit, repeatable seeding is required, consider using Python's standard `random` module or a different library that exposes such an API.","message":"The `fastrand.seed()` function was present in very early versions (e.g., v0.x) but is not part of the current public API. The PCG generator in fastrand is designed to be automatically seeded, typically from system entropy upon initialization. Attempting to seed it directly via a `seed()` function will fail.","severity":"deprecated","affected_versions":"<1.0.0 (removed). All current versions do not expose `seed()`."}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Run `pip install fastrand` in your terminal to install the library.","cause":"The fastrand library has not been installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'fastrand'"},{"fix":"Use `fastrand.pcg32bounded(N)` to get a random integer in the range [0, N-1].","cause":"Attempting to use a function name from Python's standard `random` module (`randint`) which does not exist in the `fastrand` API.","error":"AttributeError: module 'fastrand' has no attribute 'randint'"},{"fix":"Provide a positive integer argument to `pcg32bounded()`, e.g., `fastrand.pcg32bounded(100)` for numbers 0-99.","cause":"The `fastrand.pcg32bounded()` function requires a single argument, which is the upper bound (exclusive) for the random integer generation.","error":"TypeError: pcg32bounded() takes exactly one argument (0 given)"}]}