如上一篇文章所介绍的,numpy.random
模块提供了一些高效生成各种概率分布下随机数据的函数。实际上,这些随机数是伪随机数,因为他们是由具有确定性行为的算法根据随机数生成器的随机种子生成的。通过设置相同的随机种子,可以使生成的随机数相同。
numpy.random.seed :全局随机种子
当设置相同的seed,生成的随机数相同。但是seed只能一次有效,如果调用生成随机数的函数之前没有再次设置相同的seed,则生成的随机数是不同的。
In [157]: np.random.seed(0) # 设置全局随机种子为0
In [158]: np.random.rand(5)
Out[158]: array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])
In [160]: np.random.seed(0) # 再次设置相同的全局随机种子,生成的随机数相同
In [161]: np.random.rand(5)
Out[161]: array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])
In [159]: np.random.rand(5) # 不设置随机种子,两次生成的随机数不同
Out[159]: array([0.64589411, 0.43758721, 0.891773 , 0.96366276, 0.38344152])
numpy.random.RandomState: 局部随机种子
通过调用numpy.random.RandomState
可以生成一个随机数生成器,通过该生成器调用生成随机数的函数,就可以生成随机数。
In [2]: rng = np.random.RandomState(1)
In [3]: rng.rand(4)
Out[3]: array([4.17022005e-01, 7.20324493e-01, 1.14374817e-04, 3.02332573e-01])
通过传递相同的随机种子作为numpy.random.RandomState()
的参数,就可以生成相同的随机数。与全局随机种子类似,局部随机种子也是仅一次有效。必须创建一次随机数生成器,再通过生成器调用函数,只有这样才能保证生成的随机数相同。
In [2]: rng = np.random.RandomState(1)
In [4]: x = rng.rand(4)
In [5]: x
Out[5]: array([0.14675589, 0.09233859, 0.18626021, 0.34556073])
In [6]: y = rng.rand(4) # 通过生成器两次调用rand()函数生成的随机数组是不同的。
In [7]: y
Out[7]: array([0.39676747, 0.53881673, 0.41919451, 0.6852195 ])
In [8]: rng = np.random.RandomState(1)
In [9]: x = rng.rand(4)
In [10]: x
Out[10]: array([4.17022005e-01, 7.20324493e-01, 1.14374817e-04, 3.02332573e-01])
In [11]: rng = np.random.RandomState(1) # 写一次生成器,调用一次rand()函数才可以保证生成的随机数组相同。
In [12]: y = rng.rand(4)
In [13]: y
Out[13]: array([4.17022005e-01, 7.20324493e-01, 1.14374817e-04, 3.02332573e-01])
局部随机种子的独立性
np.random.seed
与np.random.RandomState
的相同点:可以通过设置相同的随机种子生成相同的随机数np.random.seed
与np.random.RandomState
的不同点:np.random.RandomState
具有独立性,即在np.random.seed
设置好的情况下,再设置np.random.RandomState
,不会影响np.random.seed
随机数的生成。
In [20]: np.random.seed(0) # 设置全局随机种子
In [21]: for i in range(3):
...: print('global {}th: '.format(i), np.random.rand(3))
...:
global 0th: [0.5488135 0.71518937 0.60276338]
global 1th: [0.54488318 0.4236548 0.64589411]
global 2th: [0.43758721 0.891773 0.96366276]
In [22]: np.random.seed(0) # 设置相同的全局随机种子
In [23]: rng = np.random.RandomState(1) # 设置局部随机种子
In [25]: for i in range(3):
...: print('global {}th: '.format(i), np.random.rand(3))
...: print('local {}th: '.format(i), rng.rand(3))
...:
global 0th: [0.5488135 0.71518937 0.60276338]
local 0th: [4.17022005e-01 7.20324493e-01 1.14374817e-04]
global 1th: [0.54488318 0.4236548 0.64589411]
local 1th: [0.30233257 0.14675589 0.09233859]
global 2th: [0.43758721 0.891773 0.96366276]
local 2th: [0.18626021 0.34556073 0.39676747]
# 设置局部随机种子之后,再设置相同的全局随机种子,生成的随机数仍然相同。两者互不影响。