简体   繁体   中英

Does a random seed set via numpy.random.seed maintain across submodules?

If I set a seed for my RNG eg numpy.random.seed(0) and I call a submodule, will the RNG's state be maintained?

eg

# some_lib.py
def do_thing():
  return numpy.random.rand()
# parent module
import some_lib
numpy.seed(0)
...
some_lib.do_thing()

Will the numpy state set by the parent be used by the child?

The seed is a global value for all uses of numpy . So as long as the child module doesn't reseed it, or pull values from it non-deterministically (effectively adjusting it to a new seed based on advancing the old), then the seed will be preserved.

Most PRNG libraries behave this way, because the alternative is pretty useless; for reproducible tests, you want to be able to set the seed once, and have everything rely on that stable seed. If there was a per-module seed, the testing module couldn't seed the PRNG used by the module being tested.

在测试中,似乎numpy的RNG状态由子进程维护。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM