Skip to content

[numpy.md] Update np.random → Generator API#549

Open
Chihiro2000GitHub wants to merge 2 commits into
mainfrom
update-rng-numpy
Open

[numpy.md] Update np.random → Generator API#549
Chihiro2000GitHub wants to merge 2 commits into
mainfrom
update-rng-numpy

Conversation

@Chihiro2000GitHub
Copy link
Copy Markdown
Collaborator

Summary

This PR migrates legacy NumPy random API calls in numpy.md as part of QuantEcon/meta#299.
All np.random.randn, np.random.binomial, and from numpy.random import uniform usages are replaced with the Generator API via np.random.default_rng().

Details

  • rng = np.random.default_rng() is introduced at the first point of use (Mutability section) and reused throughout the main lecture text.
  • Exercise solution blocks (np_ex3, np_ex4) define their own local rng to remain self-contained.
  • np_ex4 (Part 1 and Part 2, exercise and solution) retains seed 123 via np.random.default_rng(123), matching the original intent of reproducible comparison between broadcasting and for-loop results.
  • DiscreteRV in the np_ex2 solution stores self.rng = np.random.default_rng() in __init__ and uses self.rng.uniform(...) in draw.
  • No fixed seed was introduced beyond what was already present in the original.
  • No Numba-related code was found in this file.
  • Local build completed successfully (jb build lectures); numpy.md executed without errors in 30 seconds.

Notes for reviewers

One item was intentionally left unchanged and would benefit from reviewer judgment:

The prose in the Sub-packages section currently reads:

We've already seen how we can generate random variables using np.random

After this migration, the code directly below uses rng.standard_normal() and rng.binomial() rather than np.random.xxx() directly. Technically rng is still obtained from np.random.default_rng(), so the sentence is not wrong — but it may feel slightly mismatched to a reader. Happy to update the wording if you think that would be clearer.

Hi @mmcky and @HumphreyYang, I'd be grateful if you could take a look when you have time.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 9, 2026

@github-actions github-actions Bot temporarily deployed to pull request May 9, 2026 22:42 Inactive
Copy link
Copy Markdown
Member

@HumphreyYang HumphreyYang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Chihiro2000GitHub,

This looks great to me! The only small suggestion I have is that

We’ve already seen how we can generate random variables using `np.random`

seems a bit stale because we are specifically using the Generator API. I think

We've already seen how we can generate random variables using NumPy's Generator API.

would be a better description.

Another minor point is that we can potentially allow for the seed key to be passed for the exercise solution by:

class DiscreteRV:

    def __init__(self, q, seed=None):
        self.q = q
        self.Q = cumsum(q)
        self.rng = np.random.default_rng(seed)

    def draw(self, k=1):
        return self.Q.searchsorted(self.rng.uniform(0, 1, size=k))

Please let me know what you think!

- Update prose to reference NumPy's Generator API instead of np.random
- Add optional seed parameter to DiscreteRV.__init__ for reproducibility

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Chihiro2000GitHub
Copy link
Copy Markdown
Collaborator Author

Hi @HumphreyYang,

Thank you for the feedback! Both of your points make perfect sense.

I've addressed both points:

  1. Updated the prose from np.random to "NumPy's Generator API" to better reflect the current usage.
  2. Added an optional seed parameter to DiscreteRV.__init__.

Please let me know if anything else needs adjusting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants