{"id":16572690,"url":"https://github.com/lemire/fastrand","last_synced_at":"2025-05-13T10:47:36.448Z","repository":{"id":65675079,"uuid":"52118397","full_name":"lemire/fastrand","owner":"lemire","description":"Fast random number generation in an interval in Python: Up to 10x faster than random.randint.","archived":false,"fork":false,"pushed_at":"2025-05-13T04:53:53.000Z","size":74,"stargazers_count":88,"open_issues_count":0,"forks_count":13,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-05-13T05:29:19.593Z","etag":null,"topics":["performance","prng","python"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lemire.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-02-19T21:43:15.000Z","updated_at":"2025-05-13T04:53:34.000Z","dependencies_parsed_at":"2024-10-26T20:29:00.831Z","dependency_job_id":"5f2de49b-352f-4c91-9f15-33096c4a4324","html_url":"https://github.com/lemire/fastrand","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemire%2Ffastrand","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemire%2Ffastrand/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemire%2Ffastrand/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemire%2Ffastrand/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lemire","download_url":"https://codeload.github.com/lemire/fastrand/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253928163,"owners_count":21985789,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["performance","prng","python"],"created_at":"2024-10-11T21:28:18.640Z","updated_at":"2025-05-13T10:47:36.424Z","avatar_url":"https://github.com/lemire.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fastrand\n\nFast random number generation in an interval in Python using PCG: Up to 10x faster than random.randint.\n\nBlog post: [Ranged random-number generation is slow in Python](https://lemire.me/blog/2016/03/21/ranged-random-number-generation-is-slow-in-python/)\n\n\n\n\nUsage... (don't forget to type the above lines in your shell!)\n\n```python\nimport fastrand\n\nprint(\"generate an integer in [0,1001)\")\nfastrand.pcg32bounded(1001) \nprint(\"generate an integer in [100,1000]\")\nfastrand.pcg32randint(100,1000) # requires Python 3.7 or better\nprint(\"Generate a random 32-bit integer.\")\nfastrand.pcg32()\n\nif fastrand.SIXTYFOUR: # support for xorshift128+ is limited to some 64-bit platforms (linux, macos, etc.)\n    print(\"generate an integer in [0,1001)\")\n    fastrand.xorshift128plusbounded(1001) \n    print(\"generate an integer in [100,1000]\")\n    fastrand.xorshift128plusrandint(100,1000) # requires Python 3.7 or better\n    print(\"Generate a random 64-bit integer.\")\n    fastrand.xorshift128plus()\n```\n\nIt is nearly an order of magnitude faster than the alternatives:\n\n```\n\npython3 -m timeit -s 'import fastrand' 'fastrand.pcg32bounded(1001)'\n10000000 loops, best of 5: 23.6 nsec per loop\n\npython3 -m timeit -s 'import fastrand' 'fastrand.pcg32randint(100,1000)'\n10000000 loops, best of 5: 24.6 nsec per loop\n\npython3 -m timeit -s 'import random' 'random.randint(0,1000)'\n1000000 loops, best of 5: 216 nsec per loop\n\npython3 -m timeit -s 'import numpy' 'numpy.random.randint(0, 1000)'\n500000 loops, best of 5: 955 nsec per loop\n\n```\n\nThe pcg32 generator is a 32-bit generator so it generates values in the interval from `0` to `2**32-1`.\nThe xorshift128+ generator is a 64-bit generator so that it can generate values in a 64-bit range (up to `2**64-1`).\n\n\nIf you have Linux, macOS or Windows, you should be able to do just pip install...\n\n```\npip install fastrand\n```\n\nYou may need root access (sudo on macOS and Linux).\n\nIt is sometimes useful to install a specific version, you can do so as follows;\n\n```\npip install fastrand==1.2.4\n```\n\n\n\nGenerally, you can build the library as follows (if you have root):\n\n\n```bash\npython setup.py build\npython setup.py install \n```\n\nor\n\n```bash\npython setup.py build\npython setup.py install --home=$HOME\nexport PYTHONPATH=$PYTHONPATH:~/lib/python\n```\n\n\n## Changing the seed and multiple streams\n\n- You can change the seed with a function like `pcg32_seed`. The seed determines the random values you get. Be mindful that naive seeds (e.g., `int(time.time())`) can deliver poor initial randomness. A few calls to `pcg32()` may help to boost the improve the randomness. Or else you may try a better seed.\n- If you need to produce multiple streams of random numbers, merely changing the seed is not enough. You are better off using different increments by calling the `pcg32inc`. The increments should all be distinct. Note that the least significant bit of the increment is always set to 1 no matter which value you pass: so make sure your increments are distinct 31-bit values (ignoring the least significant bit).\n- You may also initialize xorshift128+ with `xorshift128plus_seed1` and `xorshift128plus_seed2`.\n\n## Reference\n\n* http://www.pcg-random.org\n* Daniel Lemire, [Fast Random Integer Generation in an Interval](https://arxiv.org/abs/1805.10941), ACM Transactions on Modeling and Computer Simulation, Volume 29 Issue 1, February 2019 \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemire%2Ffastrand","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flemire%2Ffastrand","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemire%2Ffastrand/lists"}