[numba.md] Update np.random → Generator API#550
Open
Chihiro2000GitHub wants to merge 1 commit into
Open
Conversation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Summary
This PR migrates legacy NumPy random API usage in
numba.mdas part of QuantEcon/meta#299.Details
The Numba/JIT-related classification and changes in this PR follow the guidance at https://manual.quantecon.org/styleguide/code.html#numpy-random-number-generation. I would be grateful if you could refer to the Numba section of this page when reviewing.
Case A (
update(), main text,@jit): Left unchanged. Althoughupdate()is@jit-decorated, it is called from aprangeloop incompute_long_run_median_parallel, making it unsafe to pass a shared Generator. Flagged for reviewer judgment.Case B (speed_ex1 solution,
@jit/ no parallel):rng = np.random.default_rng()placed before the@jitdefinition; signature changed tocalculate_pi(rng, n=...);np.random.uniform→rng.uniform. Call sites updated. Note: passing a Generator into a@jitfunction may require Numba to use object mode. I checked that the updated code runs as expected on my side, but I would appreciate reviewer confirmation.Case C (speed_ex2 solution, plain Python / later JIT-compiled via
jit(compute_series)): Same pattern as Case B. Signature changed tocompute_series(n, rng);np.random.uniform→rng.uniform. Both the pure Python andjit-compiled call sites updated.Case D (numba_ex3 solution,
@jit(parallel=True)+prange): Draws lifted outside theprangeloop.rng,u_draws, andv_drawsdefined before the@jit(parallel=True)definition; signature changed tocalculate_pi(u_draws, v_draws); loop length derived fromlen(u_draws).n = 1_000_000kept to match the original example size.Case E (numba_ex4 solution,
@jit(parallel=True)+prange): Legacynp.random.randn()calls kept intentionally as a narrow memory-constrained exception. Pre-allocating(M, n)shock arrays withM = 10_000_000andn = 20would require approximately 3.2 GB, which is likely beyond what most readers' machines can comfortably accommodate. A short comment has been added inside the loop explaining this.Hi @mmcky and @HumphreyYang, I'd be grateful if you could take a look when you have time.