[Numpy-discussion] seed for new random number generator

2022-06-19 Thread Pieter Eendebak
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?

With kind regards,
Pieter Eendebak
___
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


[Numpy-discussion] Re: seed for new random number generator

2022-06-19 Thread Robert Kern
On Sun, Jun 19, 2022 at 9:37 AM Pieter Eendebak 
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: arch...@mail-archive.com


[Numpy-discussion] Re: seed for new random number generator

2022-06-19 Thread Kevin Sheppard
On Sun, Jun 19, 2022 at 4:36 PM Robert Kern  wrote:

> On Sun, Jun 19, 2022 at 9:37 AM Pieter Eendebak 
> 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


[Numpy-discussion] next NumPy community meeting

2022-06-19 Thread Inessa Pawson
The next NumPy community meeting will be held this Wednesday, June 22nd at
18:00 (6 pm) UTC.

Join us via Zoom: https://berkeley.zoom.us/j/762261535
Everyone is welcome and encouraged to attend.

To add to the meeting agenda the topics you’d like to discuss, follow the
link: https://hackmd.io/76o-IxCjQX2mOXO_wwkcpg?both

Cheers,
Inessa

Inessa Pawson
Contributor Experience Lead | NumPy
https://numpy.org/
Twitter: @inessapawson
___
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