[Numpy-discussion] NEP: Random Number Generator Policy

Nathaniel Smith njs at pobox.com
Mon Jun 11 03:45:39 EDT 2018


On Sun, Jun 10, 2018 at 11:53 PM, Ralf Gommers <ralf.gommers at gmail.com> wrote:
>
> On Sun, Jun 10, 2018 at 11:15 PM, Robert Kern <robert.kern at gmail.com> wrote:
>>
>> The intention of this code is to shuffle two same-length sequences in the
>> same way. So now if I write my code well to call np.random.seed() once at
>> the start of my program, this function comes along and obliterates that with
>> a fixed seed just so it can reuse the seed again to replicate the shuffle.
>
>
> Yes, that's a big no-no. There are situations conceivable where a library
> has to set a seed, but I think the right pattern in that case would be
> something like
>
> old_state = np.random.get_state()
> np.random.seed(some_int)
> do_stuff()
> np.random.set_state(**old._state)

This will seem to work fine in testing, and then when someone tries to
use your library in a multithreaded program everything will break in
complicated and subtle ways :-(. I really don't think there's any
conceivable situation where a library (as opposed to an application)
can correctly use the global random state.

-n

-- 
Nathaniel J. Smith -- https://vorpus.org


More information about the NumPy-Discussion mailing list