Skip to content

[functions.md] Update np.random → Generator API#541

Open
Chihiro2000GitHub wants to merge 3 commits intomainfrom
update-functions-rng
Open

[functions.md] Update np.random → Generator API#541
Chihiro2000GitHub wants to merge 3 commits intomainfrom
update-functions-rng

Conversation

@Chihiro2000GitHub
Copy link
Copy Markdown
Collaborator

@Chihiro2000GitHub Chihiro2000GitHub commented Apr 26, 2026

This PR updates lectures/functions.md as part of the Numpy random API migration described in QuantEcon/meta#299.

Approach

A single rng = np.random.default_rng() is introduced where random draws first appear in the main lecture flow (the "4.4.1. Random Draws" section) and reused across subsequent code cells, following the natural notebook execution order. This avoids redundant redefinition while keeping the changes minimal.

For exercise solution blocks, which are intended to be self-contained, rng = np.random.default_rng() is defined locally at the top of each relevant solution cell. This aligns with the strategy I took in the previous pull request on python_by_example.md: #538

Point for review

In Section 4.4.2. ("Adding Conditions"), the lecture demonstrates passing a callable as an argument to generate_data(). After this migration, the object passed is rng.uniform, which is technically a method rather than a function. As a result, a few sentences related to this - such as "by just passing the desired generator type as a function," "This object is a function", and "bound to the function rng.uniform" - are no longer strictly accurate.

I chose to leave these sentences unchanged for now, as this is an introductory programming lecture and I was uncertain whether introducing terms like "method," "callable," or "function-like object" would help or confuse readers who are likely encountering these concepts for the first time.

I would appreciate the reviewer's guidance on how to handle this wording. I am happy to revise the sentences based on your feedback and will request a re-review after making any requested changes. I will only merge once you are satisfied with the result.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 26, 2026

@github-actions github-actions Bot temporarily deployed to pull request April 26, 2026 07:30 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.

Many thanks @Chihiro2000GitHub,

This looks great overall!

One small wording issue: after this change, technically speaking, rng.uniform is a Generator method rather than a standalone function, so the text saying "This object is a function" / "the function rng.uniform" is slightly inaccurate.

Could we reword that section to say that rng.uniform is a callable, i.e., an object that can be called using parentheses?

Then the final sentence could say "passing a function, method, or other callable object" rather than only "passing a function."

@github-actions github-actions Bot temporarily deployed to pull request April 29, 2026 03:57 Inactive
@Chihiro2000GitHub
Copy link
Copy Markdown
Collaborator Author

Thanks very much for the clear guidance, @HumphreyYang !
I revised the wording to describe rng.uniform as a callable and updated the final sentence accordingly.

I would be grateful if you could take another look when you have time.

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.

Thanks @Chihiro2000GitHub, nice changes.

Please also update this sentence:

In the context of our program, the ability to bind new names to functions
means that there is no problem passing a function as an argument to another
function
---as we did above.

to something like:

In the context of our program, the ability to bind names to functions, or more generally to callable objects, means that there is no problem passing one callable object as an argument to another callable --- as we did with rng.uniform above.

Comment thread lectures/functions.md Outdated
Now, there are several ways that we can simplify the code above.

For example, we can get rid of the conditionals all together by just passing the desired generator type *as a function*.
For example, we can get rid of the conditionals all together by just passing the desired generator type as a function, method, or other callable object.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
For example, we can get rid of the conditionals all together by just passing the desired generator type as a function, method, or other callable object.
For example, we can get rid of the conditionals all together by just passing the desired generator type as a function, method, or other [callable](https://typing.python.org/en/latest/spec/callables.html) object.

@Chihiro2000GitHub
Copy link
Copy Markdown
Collaborator Author

Chihiro2000GitHub commented Apr 30, 2026

Thanks @HumphreyYang.
I made the remaining wording updates locally, including the suggested change above, and pushed the update. Once you’re happy with the result, I’ll go ahead and merge it.

@github-actions github-actions Bot temporarily deployed to pull request April 30, 2026 03:53 Inactive
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