numpy.random.seed() 每次总是给出相同的随机数吗? [英] Does numpy.random.seed() always give the same random number every time?

查看:534
本文介绍了numpy.random.seed() 每次总是给出相同的随机数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如果我使用相同的种子,numpy.random.seed(seed) 将输出相同的数字.我的问题是,这会随着时间的推移而改变吗?如果我明天再次尝试调用它,它是否仍会输出与昨天相同的一组随机数?

解决方案

np.random 文档描述了所使用的 PRNG.显然,MT19937 有部分切换PCG64 在最近的过去.如果您想要一致性,您需要:

  1. 修复使用的 PRNG,以及
  2. 确保您使用的是本地句柄(例如 RandomStateGenerator),这样对其他 外部库的任何更改都不会自己调用 np.random 全局变量来搞砸.

在本例中,我们使用较新的BitGenerator API,提供各种 PRNG 的选择.

from numpy.random import Generator, PCG64rg = 发电机(PCG64(1234))

可以如下使用:

<预><代码>>>>rg.uniform(0, 10, 10)数组([9.767, 3.802, 9.232, 2.617, 3.191, 1.181, 2.418, 3.185, 9.641,2.636])

如果我们多次重新运行它(即使在同一个 REPL 中!),我们将始终获得相同的随机数生成器.PCG64 与 MT19937 一样,提供以下保证:

<块引用>

兼容性保证

PCG64 保证固定的种子并将始终产生相同的随机整数流.

虽然,正如@user2357112 支持 Monica 所指出的那样,对使用随机整数序列(例如 np.random.Generator.uniform)的随机 API 函数的更改仍然存在技术上可行,但不太可能.

为了生成多个生成器,可以利用SeedSequence.spawn(k)生成k个不同的SeedSequences.这对于一致的并发计算很有用:

from numpy.random import Generator, PCG64, SeedSequencesg = 种子序列(1234)rgs = [Generator(PCG64(s)) for s in sg.spawn(10)]

I know that numpy.random.seed(seed) will output the same numbers if I use the same seed. My question is, does this change over time? If I try to call it again tomorrow, will it still output the same set of random numbers as yesterday?

解决方案

The np.random documentation describes the PRNGs used. Apparently, there was a partial switch from MT19937 to PCG64 in the recent past. If you want consistency, you'll need to:

  1. fix the PRNG used, and
  2. ensure that you're using a local handle (e.g. RandomState, Generator) so that any changes to other external libraries don't mess things up by calling np.random globals themselves.

In this example, we make use of the newer BitGenerator API, which provides a selection of various PRNGs.

from numpy.random import Generator, PCG64

rg = Generator(PCG64(1234))

Which may be used as follows:

>>> rg.uniform(0, 10, 10)
array([9.767, 3.802, 9.232, 2.617, 3.191, 1.181, 2.418, 3.185, 9.641,
       2.636])

If we re-run this any number of times (even within the same REPL!), we will always obtain the same random number generator. PCG64, like MT19937, provides the following guarantee:

Compatibility Guarantee

PCG64 makes a guarantee that a fixed seed and will always produce the same random integer stream.

Though, as @user2357112 supports Monica noted, changes to the random API functions that use the random integer sequence (e.g. np.random.Generator.uniform) are still technically possible, though unlikely.

In order to generate multiple generators, one can make use of SeedSequence.spawn(k) to generate k different SeedSequences. This is useful for consistent concurrent computations:

from numpy.random import Generator, PCG64, SeedSequence

sg = SeedSequence(1234)
rgs = [Generator(PCG64(s)) for s in sg.spawn(10)]

这篇关于numpy.random.seed() 每次总是给出相同的随机数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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