Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Updated Random Number Generation in Numpy #19706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

rajpratyush
Copy link
Contributor

@rajpratyush rajpratyush commented Mar 15, 2021

PR Summary

closes #19670

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

Copy link
Member

@dstansby dstansby left a comment

Choose a reason for hiding this comment

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

This looks great overall. Could you change the variable name of the generator from r to rng, or something else a bit more descriptive?

@dstansby dstansby added this to the v3.5.0 milestone Mar 15, 2021
@rajpratyush
Copy link
Contributor Author

Surely

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

While it is generally good to use state-of-the art patterns, IMHO we we should push the change out further, likely to 3.6 or so.

This code doesn't work for numpy < 1.17. Since Matplotlib 3.5 will require numpy >= 1.17, we're technically ok with using it in the docs. However, I project that we will have lot's of users who are still on older numpy versions and will copy examples from the stable docs, only to find that np.random.default_rng() doesn't exist and the example does not run. IMHO the downside of likely confusing users with non-running examples weighs heavier than not using the latest recommended API. This balance will tip eventually when more and more users have migrated to numpy 1.17+.

I therefore block until we have decided if we really want this already in Matplotlib 3.5.

Side-note: There are many many more uses of np.random in the examples, all of which should be switched if we want to switch.

@rajpratyush
Copy link
Contributor Author

@timhoffm how about we mention it specifically as in hist.py file for developers running on numpy>=1.17 they should adopt this practice while the developers running on numpy<1.17 should adopt with the previous and since we will eventually have to switch to numpy >= 1.17. I propose lets keep both the changes till we start using numpy >=1.17 then we can remove the previous verison.

@jklymak
Copy link
Member

jklymak commented Mar 16, 2021

I don't even under stand why numpy has done this. Like sure make other generators available, but why a whole new API?

@rajpratyush
Copy link
Contributor Author

@jklymak Same here.

@timhoffm
Copy link
Member

There are fundamental changes https://numpy.org/doc/stable/reference/random/new-or-different.html#new-or-different.
A new API is a clean way of both giving straight forward access to the new and different random generators while maintaining full backward-compatibility for existing users. - Maybe not the only way, but a valid approach.

@rajpratyush
Copy link
Contributor Author

@timhoffm what are your views on my proposed solution ?

@timhoffm
Copy link
Member

@timhoffm how about we mention it specifically as in hist.py file for developers running on numpy>=1.17 they should adopt this practice while the developers running on numpy<1.17 should adopt with the previous

I don't think we should clutter our examples with notes on different API versions of other libraries.

Note that while the new random generator is recommended for new code, random.random() is not deprecated and can be used for the time being.

I've put the topic on the agenda for the developer call tomorrow. We'll discuss it there among the core devs. You are welcome to join the call if you want to take part in the discussion.

@tacaswell
Copy link
Member

There are still likely many users who are not on the latest numpy, the old way is not being deprecated, the change is to enable things that we do not need (and will never need) in our context, and if we change it in one place we should change it everywhere (which will be a lot of thrashing).

In another few years we could re-consider, but for now we are going to respectfully decline this change. Thank you for your effort on this @rajpratyush!

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.

The Recommended Random Number Generation In NumPy
5 participants