[Numpy-discussion] reseed random generator (1.19)
Robert Kern
robert.kern at gmail.com
Mon Jun 29 10:51:23 EDT 2020
On Mon, Jun 29, 2020 at 8:02 AM Neal Becker <ndbecker2 at gmail.com> wrote:
> I was using this to reset the generator, in order to repeat the same
> sequence again for testing purposes.
>
In general, you should just pass in a new Generator that was created with
the same seed.
def function_to_test(rg):
x = rg.standard_normal()
...
SEED = 12345...
rg = np.random.default_rng(SEED)
function_to_test(rg)
rg = npp.random.default_rng(SEED)
function_to_test(rg)
Resetting the state of the underlying BitGenerator in-place is possible, as
Kevin showed, but if you can refactor your code so that there isn't a
persistent Generator object between these runs, that's probably better.
It's a code smell if you can't just pass in a fresh Generator; in general,
it means that your code is harder to use, not just because we don't expose
an in-place seed() method.
--
Robert Kern
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20200629/8a3a12d2/attachment-0001.html>
More information about the NumPy-Discussion
mailing list