Skip to content
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

Support numpy.random.default_rng #558

Open
blackberryshortcake opened this issue Jul 11, 2023 · 4 comments
Open

Support numpy.random.default_rng #558

blackberryshortcake opened this issue Jul 11, 2023 · 4 comments

Comments

@blackberryshortcake
Copy link

blackberryshortcake commented Jul 11, 2023

Description

The readme says:

If numpy is installed, its global random state is reset at the start of every test.

Nowadays, numpy recommends numpy.random.default_rng, which returns a PRNG object and using its methods, e.g.

import numpy as np

# not passing a seed -> implicitly creates a seed from global state
rng = np.random.default_rng()

sample = rng.some_probability_distribution()

They call numpy.random.some_probability_distribution the legacy way.
Randomly supports the legacy way. It reseeds with numpy.random.seed, which doesn't affect dedicated generators (which is what one gets from default_rng).

It'd be great if randomly supported default_rng or if the README was adapted.

@blackberryshortcake
Copy link
Author

blackberryshortcake commented Jul 11, 2023

As a workaround, one can use

rng = np.random.default_rng(np.random.randint(10000))

or similar. The seed and therefore rng is controlled by randomly.

@adamchainz
Copy link
Member

Thanks for the note. I haven’t kept up with numpy development since creating pytest-randomly.

It doesn’t look like we would be able to support default_rng. There can be may rng instances in user code and we don’t know where they are to reset their seeds.

We could perhaps document a pattern for using randomly’s seed to reset other rngs. I have been thinking some hook or pytest fixture could be better than the existing entry points solution. Would that work for you?

Maybe we could remove the default numpy support too, since it’s legacy. Updating this library for new python versions always stalls waiting for new numpy versions.

@blackberryshortcake
Copy link
Author

It doesn’t look like we would be able to support default_rng. There can be may rng instances in user code and we don’t know where they are to reset their seeds.

Yes, I've come to the same conclusion.

We could perhaps document a pattern for using randomly’s seed to reset other rngs. I have been thinking some hook or pytest fixture could be better than the existing entry points solution. Would that work for you?

That would be a good solution for my purposes. At work, we already use a pytest fixture to provide (new and freshly seeded) numpy RNGs on demand, but we have a custom solution to set the initial seed. It works well, but I'm still interested in randomly because it has a better interface and can shuffle tests.

Maybe we could remove the default numpy support too, since it’s legacy. Updating this library for new python versions always stalls waiting for new numpy versions.

Do note that numpy has "no plans to remove" the legacy method.

@adamchainz
Copy link
Member

Do note that numpy has "no plans to remove" the legacy method.

Okay, I've documented this as legacy in 2cef1c4.

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

No branches or pull requests

2 participants