Support numpy.random.default_rng
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.
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.
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.
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.
Do note that numpy has "no plans to remove" the legacy method.
Okay, I've documented this as legacy in 2cef1c42f33fb65dc9d9c54998717b45750cbef1.