[python_by_example.md] Update np.random → Generator API#538
[python_by_example.md] Update np.random → Generator API#538Chihiro2000GitHub merged 1 commit intomainfrom
Conversation
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.
✅ Translation sync completed (zh-cn)Target repo: QuantEcon/lecture-python-programming.zh-cn
|

This PR updates legacy NumPy random API usage in
python_by_example.mdto the recommended Generator API, as part of QuantEcon/meta#299I 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.