(numpy)个随机种子有范围吗? [英] Is there a scope for (numpy) random seeds?

查看:253
本文介绍了(numpy)个随机种子有范围吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与有关Python中随机种子的数量?. 对于上述问题,需要说明的是,在random的模块中存在一个(隐藏的)全局Random()实例.

My question is related to What is the scope of a random seed in Python? . In the case of above question, it is clarified that there is a (hidden) global Random() instance in the module for random.

1)我想澄清的是,在一个模块中设置随机种子是否会导致它成为 other 模块中的随机种子,以及是否需要注意某些事情.

1) I would like to clarify whether setting the random seed in one module will cause this to be the random seed in other modules and whether there are certain things to be aware of.

例如: 给定:moduleA.py,moduleB.py

For instance: Given: moduleA.py, moduleB.py

moduleA.py:

moduleA.py:

import random 
import moduleB
random.seed(my_seed)
moduleB.randomfct()

moduleB.py:

moduleB.py:

import random 
def randomfct():
    #do_things_using_random

moduleB是否也使用my_seed,还是我必须将种子传递给moduleB.py并重新设置?

Does moduleB also use my_seed, or do I have to pass the seed to moduleB.py and set it again?

2)设置随机种子/导入的顺序是否起作用?

2) Does the order of setting the random seed / importing play any role?

例如在moduleA.py中:

import random 
random.seed(my_seed)
import moduleB

3)设置numpy随机种子也是这种情况,例如np.random.seed(42)?

3) Is this also the case for setting numpy random seeds, e.g. np.random.seed(42)?

推荐答案

CPython random.py实现非常易读.我建议您看一下: https://github.com/python/cpython/blob/3.6/Lib/random.py

The CPython random.py implementation is very readable. I recommend having a look: https://github.com/python/cpython/blob/3.6/Lib/random.py

无论如何,该版本的python创建一个全局random.Random()对象并将其直接分配给random模块.该对象包含一个seed(a)方法,该方法充当模块功能当您致电random.seed(a)时.因此,种子状态将在整个程序中共享.

Anyway, that version of python creates a global random.Random() object and assigns it directly to the random module. This object contains a seed(a) method which acts as a module function when you call random.seed(a). Thus the seed state is shared across your entire program.

1)是. moduleAmoduleB使用相同的种子.在moduleA中导入random会创建全局random.Random()对象.在moduleB中重新导入它只会为您提供相同的模块并维护最初创建的random.Random()对象.

1) Yes. moduleA and moduleB uses the same seed. Importing random in moduleA creates the global random.Random() object. Reimporting it in moduleB just gives you the same module and maintains the originally created random.Random() object.

2)否.不是您所举的例子,但总的来说,这很重要.在将种子设置为moduleA之前,您可能会先使用moduleB,这样就不会设置种子.

2) No. Not in the example you gave, but in general yes it can matter. You might use moduleB before you set the seed in moduleA thus your seed wasn't set.

3)很难说.更复杂的代码库.也就是说,我认为它的工作方式相同. numpy的作者确实必须 try 来使其工作方式不同于python实现中的工作方式.

3) Hard to tell. Much more complicated code base. That said, I would think it works the same way. The authors of numpy would really have to try to make it work in a different way than how it works in the python implementation.

通常,如果您担心种子状态,建议您创建自己的随机对象并将其传递以生成随机数.

In general, if you are worried about seed state, I recommend creating your own random objects and pass them around for generating random numbers.

这篇关于(numpy)个随机种子有范围吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆