[python_by_example.md] Update np.random → Generator API#538
Open
Chihiro2000GitHub wants to merge 1 commit intomainfrom
Open
[python_by_example.md] Update np.random → Generator API#538Chihiro2000GitHub wants to merge 1 commit intomainfrom
Chihiro2000GitHub wants to merge 1 commit intomainfrom
Conversation
Member
There was a problem hiding this comment.
Many thanks, @Chihiro2000GitHub, and congratulations on your first QuantEcon PR!
This looks great to me. Your reasoning about redefining rng in solutions makes sense, and it would be nice to apply this rule consistently across lectures when making similar changes.
Just one minor suggestion: it would be helpful if you could reference the issue you are trying to resolve (or partially resolve, in this case):
QuantEcon/meta#299
Aside from that, this is a great PR!
Please feel free to move on to the rest of the lectures in the series. Also, remember to create a new branch based on main.
HumphreyYang
approved these changes
Apr 23, 2026
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.
This PR updates legacy NumPy random API usage in
python_by_example.mdto the recommended Generator API.I introduced a local
rng = np.random.default_rng()and replaced the relevant legacy random calls using the new Generator API.In the main lecture flow, I introduced
rngonly once and then reused it in subsequent cells, in order to keep the structure and presentation as close to the original as possible.For the exercise solutions, I took a slightly different approach and introduced
rngwithin each solution block. My reasoning was that these solutions seemed more natural as relatively self-contained answer units, especially since the exercise statements explicitly specify the relevant imports. That said, if this is not the preferred approach, it should be easy to revise by removing those localrngdefinitions.I also updated the first sentence in Section 3.3.1.3 (“Subpackages”), since it explicitly referred to the legacy line
np.random.randn(100)and would otherwise have become inconsistent with the revised code.I built the lecture locally to check that the changes work as expected.
I’d be very glad to hear any thoughts, especially on the choice to define
rngseparately within each exercise solution block.