On Sun, Jun 19, 2022 at 4:36 PM Robert Kern <robert.k...@gmail.com> wrote:
> On Sun, Jun 19, 2022 at 9:37 AM Pieter Eendebak <pieter.eende...@gmail.com> > wrote: > >> Hi everyone, >> >> The new numpy random interface (e.g. r=numpy.random.default_rng; >> r.random) is much faster than the old one (e.g. np.random.random). When >> converting code from the old style to the new style I miss having a way to >> set the seed of the RNG >> >> I tried: >> >> rng.bit_generator = np.random.PCG64(seed=42) # fails, with good error >> message >> rng.bit_generator.state['state']['state']=42 # has no effect, perhaps >> make this dict read-only? >> >> Is there a way to set the seed without creating a new RNG object? >> > > We generally recommend just creating a new Generator and passing that > around in almost all cases. Whenever that can possibly be made to work, > please do that. The use of np.random.seed() is usually a code smell > indicating that someone was working around the fact that there was just the > one global underneath np.random.random() et al. When you don't have the > constraint of a single global instance, it's almost always going to be > better to use multiple instances; you don't need that workaround anymore. > > There are ways to punch in a new BitGenerator into an existing Generator, > if you must, and also ways to punch in a state dict into the BitGenerator, > but these are largely for various meta-programming tasks like serialization > rather than day-to-day use of pseudorandom numbers. If you are converting > old code that happened to use np.random.seed(), it is almost certainly not > one of these tasks. > > If you want to describe your use case more specifically, I can give you > some more guidance on good patterns to replace it with the new system. > > -- > Robert Kern > _______________________________________________ > NumPy-Discussion mailing list -- numpy-discussion@python.org > To unsubscribe send an email to numpy-discussion-le...@python.org > https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ > Member address: kevin.k.shepp...@gmail.com At some point we had a seed implementation on the BitGenerator. This was benchmarked against creating a fresh BitGenerator and there was no measurable performance gain to the seed() method over creating a fresh BitGenerator. If you want to reseed, the simplest method is to simply call default_rng again. import numpy as np SEED = 382193802183092174983 rg = np.random.default_rng(SEED) rg.standard_normal((1000,100)) # Do stuff rg = np.random.default_rng(SEED) rg.standard_normal((1000,100)) Kevin
_______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-le...@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: arch...@mail-archive.com