如何取消numpy seed()的影响? [英] How to cancel the effect of numpy seed()?

查看:454
本文介绍了如何取消numpy seed()的影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在程序的第一部分中使用np.random.seed()并在第二部分中将其取消. 再次,

I would like to use np.random.seed() in the first part of my program and cancel it in the second part. Again,

  • 在我的python文件的第一部分中,我希望在每次执行时生成相同的随机数
  • 在第二部分中,我希望每次执行时生成不同的随机数

推荐答案

在第一部分中,使用常量(例如,常量)初始化种子. 0:

In the first part initialize the seed with a constant, e.g. 0:

numpy.random.seed(0)

在第二部分中,使用时间初始化种子:

In the second part initialize the seed with time:

import time
t = 1000 * time.time() # current time in milliseconds
np.random.seed(int(t) % 2**32)

(种子必须在0到2 ** 32-1之间)

(the seed must be between 0 and and 2**32 - 1)

注意:通过不带任何参数(即新的(伪)不可预测序列)的np.random.seed()调用,即可获得类似的效果.

Note: you obtain a similar effect by calling np.random.seed() with no arguments, i.e. a new (pseudo)-unpredictable sequence.

每次使用相同的常量初始化种子时,都会获得相同的数字序列:

Each time you initialize the seed with the same constant, you get the same sequence of numbers:

>>> np.random.seed(0)
>>> [np.random.randint(10) for _ in range(10)]
[5, 0, 3, 3, 7, 9, 3, 5, 2, 4]
>>> [np.random.randint(10) for _ in range(10)]
[7, 6, 8, 8, 1, 6, 7, 7, 8, 1]
>>> np.random.seed(0)
>>> [np.random.randint(10) for _ in range(10)]
[5, 0, 3, 3, 7, 9, 3, 5, 2, 4]

因此,以当前的毫秒数初始化会为您提供一些伪随机序列.

Hence initalizing with the current number of milliseconds gives you some pseudo-random sequence.

这篇关于如何取消numpy seed()的影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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