A few code-quality follow-ups noticed across the recent np.random → numpy.random.Generator migration PRs (#538, #541, #544, #545, #547, #548, #549) — surfaced by Copilot during the corresponding translation-sync reviews in the Farsi target repo. Each is a small fix; grouping in one issue for convenience.
1. python_by_example.md — stale "three lines" wording
In the Random Draws section, the narrative says:
Returning to our program that plots white noise, the remaining three lines after the import statements are
But the snippet now has four lines (rng = np.random.default_rng() was added). Either update the count or include rng = ... in the snippet so the description matches.
2. functions.md — rng referenced in cells where it isn't defined
In Applications → Random sampling, several code cells call rng.standard_normal() / rng.uniform() but don't define rng in the same cell. If the reader runs the cell in isolation (or restarts the kernel), they get NameError: rng is not defined.
Suggested fix: either add rng = np.random.default_rng() to each affected cell, move the RNG setup to the shared imports cell at the top, or pass an RNG into generate_data() to avoid hidden cell-order dependencies.
3. scipy.md — same rng-scoping issue
The linregress example uses x = rng.standard_normal(200) / y = ... rng.standard_normal(200) without defining rng in the same cell/section. Same fix as above (define locally, or document the dependency).
4. numpy.md — DiscreteRV.__init__ docstring missing seed
DiscreteRV.__init__ now accepts a seed parameter and initializes self.rng:
def __init__(self, q, seed=None):
\"\"\"
The argument q is a NumPy array, or array like, nonnegative and sums
to 1
\"\"\"
The docstring only mentions q. Worth documenting what seed controls (and noting that seed=None yields non-reproducible draws).
5. (Bonus) about_py.md — inconsistent "Pytorch" vs "PyTorch"
The file uses both "PyTorch" and "Pytorch" in different paragraphs (and the line Pytorch is just one of several Python libraries for deep learning and AI. near line 142). Standardize on "PyTorch" everywhere.
6. polars.md — legacy np.random.* API (added 2026-08-04)
The Polars lecture (#408, merged 2026-07-30) introduced eight call sites using the legacy API — np.random.seed / np.random.randn / np.random.choice / np.random.rand (lines 81, 117, 466, 468–472) — the exact pattern the series-wide Generator migration above removed. Flagged as Finding 3 in the #408 review but never filed as an issue; tracked here so a single cleanup PR covers all six items.
Happy to send PRs for any/all of these — flagging here first so the team can decide scope/priority.
A few code-quality follow-ups noticed across the recent
np.random→numpy.random.Generatormigration PRs (#538, #541, #544, #545, #547, #548, #549) — surfaced by Copilot during the corresponding translation-sync reviews in the Farsi target repo. Each is a small fix; grouping in one issue for convenience.1.
python_by_example.md— stale "three lines" wordingIn the Random Draws section, the narrative says:
But the snippet now has four lines (
rng = np.random.default_rng()was added). Either update the count or includerng = ...in the snippet so the description matches.2.
functions.md—rngreferenced in cells where it isn't definedIn Applications → Random sampling, several code cells call
rng.standard_normal()/rng.uniform()but don't definerngin the same cell. If the reader runs the cell in isolation (or restarts the kernel), they getNameError: rng is not defined.Suggested fix: either add
rng = np.random.default_rng()to each affected cell, move the RNG setup to the shared imports cell at the top, or pass an RNG intogenerate_data()to avoid hidden cell-order dependencies.3.
scipy.md— samerng-scoping issueThe
linregressexample usesx = rng.standard_normal(200)/y = ... rng.standard_normal(200)without definingrngin the same cell/section. Same fix as above (define locally, or document the dependency).4.
numpy.md—DiscreteRV.__init__docstring missingseedDiscreteRV.__init__now accepts aseedparameter and initializesself.rng:The docstring only mentions
q. Worth documenting whatseedcontrols (and noting thatseed=Noneyields non-reproducible draws).5. (Bonus)
about_py.md— inconsistent "Pytorch" vs "PyTorch"The file uses both "PyTorch" and "Pytorch" in different paragraphs (and the line
Pytorch is just one of several Python libraries for deep learning and AI.near line 142). Standardize on "PyTorch" everywhere.6.
polars.md— legacynp.random.*API (added 2026-08-04)The Polars lecture (#408, merged 2026-07-30) introduced eight call sites using the legacy API —
np.random.seed/np.random.randn/np.random.choice/np.random.rand(lines 81, 117, 466, 468–472) — the exact pattern the series-wide Generator migration above removed. Flagged as Finding 3 in the #408 review but never filed as an issue; tracked here so a single cleanup PR covers all six items.Happy to send PRs for any/all of these — flagging here first so the team can decide scope/priority.