Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
p-sawicki
approved these changes
May 8, 2026
| ModDesc( | ||
| "librt.random", | ||
| ["random/librt_random.c"], | ||
| ["random/librt_random.h", "random/librt_random_api.h", "random/librt_random_api.c"], |
Collaborator
There was a problem hiding this comment.
the _api files shouldn't be necessary when building the module as a shared library.
for more information, see https://pre-commit.ci
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The stdlib
randommodule is fairly often used in performance critical code, and it's not super efficient. Addlibrt.randomwith a subset of the stdlib module interface that is optimized for performance when compiled.Use ChaCha8 as the algorithm. Based on some research, this is a modern, high-quality PRNG algorithm. It's used by Go
math/rand/v2, among others.This is a non-cryptographic PRNG, similar to the stdlib
randommodule (but this uses a different algorithm).I used Claude Code and Codex to write all the code, but I iterated on it quite a lot and did a bunch of manual validation and code review. I also asked Codex to explicitly check that the ChaCha8 implementation is correct by comparing it to a reference implementation.
Use thread-local RNG state for module-level functions to enable good scaling in free-threaded builds. There's some extra complexity from having to free the state at thread exit.
I asked a LLM to generate and run a benchmark, and here are the results on 3.14:
choice()was replaced withrandrangewhen usinglibrt, since we don't provide it as part of this fairly minimal API.