使用 randn 在 matlab 和 python 中生成相同的数组
produce same array in matlab and python with randn
你好,我想在 python 和 matlab 中生成相同的向量,但我无法得到它。
有人知道怎么做吗?
我的python代码是:
np.random.seed(1337)
A = np.random.randn(1,3)
A = array([[-0.70318731, -0.49028236, -0.32181433]])
我的matlab代码是:
rng(1337, 'twister');
A = randn(1,3)
A = -0.7832 -0.7012 -0.7178
我想给出相同的向量...
MATLAB 和 Python/NumPy 都按照您的方式配置和使用,使用相同的伪随机数生成器。此生成器生成相同的数字序列:
>> format long
>> rng(1337, 'twister');
>> rand(1,3)
ans =
0.262024675015582 0.158683972154466 0.278126519494360
>>> np.random.seed(1337)
>>> np.random.rand(1,3)
array([[0.26202468, 0.15868397, 0.27812652]])
所以似乎是从随机流中产生正态分布值的算法不同。 There are many different algorithms to produce normally-distributed values from a random stream, and the MATLAB documentation doesn't mention which one it uses. NumPy does mention at least one method:
The Box-Muller method used to produce NumPy’s normals is no longer available in Generator
. It is not possible to reproduce the exact random values using Generator
for the normal distribution or any other distribution that relies on the normal such as the RandomState.gamma
or RandomState.standard_t
. If you require bitwise backward compatible streams, use RandomState
.
简而言之,NumPy 有一个新的随机数系统 (Generator
),旧系统仍然可用 (RandomState
)。这两个系统使用不同的算法将随机流转换为正态分布的数字:
>>> r = np.random.RandomState(1337)
>>> r.randn(1,3) # same as np.random.randn()
array([[-0.70318731, -0.49028236, -0.32181433]])
>>> g = np.random.Generator(np.random.MT19937(1337))
>>> g.normal(size=(1,3))
array([[-1.22574554, -0.45908464, 0.77301878]])
r
和 g
都产生相同的随机流(使用具有相同种子的 MT19937 生成器),但不同的正态分布随机数。
我找不到 Generator.normal
使用的算法。
你好,我想在 python 和 matlab 中生成相同的向量,但我无法得到它。 有人知道怎么做吗?
我的python代码是:
np.random.seed(1337)
A = np.random.randn(1,3)
A = array([[-0.70318731, -0.49028236, -0.32181433]])
我的matlab代码是:
rng(1337, 'twister');
A = randn(1,3)
A = -0.7832 -0.7012 -0.7178
我想给出相同的向量...
MATLAB 和 Python/NumPy 都按照您的方式配置和使用,使用相同的伪随机数生成器。此生成器生成相同的数字序列:
>> format long
>> rng(1337, 'twister');
>> rand(1,3)
ans =
0.262024675015582 0.158683972154466 0.278126519494360
>>> np.random.seed(1337)
>>> np.random.rand(1,3)
array([[0.26202468, 0.15868397, 0.27812652]])
所以似乎是从随机流中产生正态分布值的算法不同。 There are many different algorithms to produce normally-distributed values from a random stream, and the MATLAB documentation doesn't mention which one it uses. NumPy does mention at least one method:
The Box-Muller method used to produce NumPy’s normals is no longer available in
Generator
. It is not possible to reproduce the exact random values usingGenerator
for the normal distribution or any other distribution that relies on the normal such as theRandomState.gamma
orRandomState.standard_t
. If you require bitwise backward compatible streams, useRandomState
.
简而言之,NumPy 有一个新的随机数系统 (Generator
),旧系统仍然可用 (RandomState
)。这两个系统使用不同的算法将随机流转换为正态分布的数字:
>>> r = np.random.RandomState(1337)
>>> r.randn(1,3) # same as np.random.randn()
array([[-0.70318731, -0.49028236, -0.32181433]])
>>> g = np.random.Generator(np.random.MT19937(1337))
>>> g.normal(size=(1,3))
array([[-1.22574554, -0.45908464, 0.77301878]])
r
和 g
都产生相同的随机流(使用具有相同种子的 MT19937 生成器),但不同的正态分布随机数。
我找不到 Generator.normal
使用的算法。