一致地创建相同的随机numpy数组 [英] Consistently create same random numpy array

查看:85
本文介绍了一致地创建相同的随机numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在等待另一位开发人员完成一段代码,该代码将返回形状为(100,2000)的np数组,其值为-1,0或1.

I am waiting for another developer to finish a piece of code that will return an np array of shape (100,2000) with values of either -1,0, or 1.

在此期间,我想随机创建一个具有相同特征的数组,以便可以在开发和测试中抢先一步.事实是,我希望这个随机创建的数组每次都相同,这样我就不会针对每次重新运行过程都会不断更改其值的数组进行测试.

In the meantime, I want to randomly create an array of the same characteristics so I can get a head start on my development and testing. The thing is that I want this randomly created array to be the same each time, so that I'm not testing against an array that keeps changing its value each time I re-run my process.

我可以像这样创建我的数组,但是有一种创建它的方法,以便每次都相同.我可以腌制然后解开物体,但是想知道是否还有另一种方法.

I can create my array like this, but is there a way to create it so that it's the same each time. I can pickle the object and unpickle it, but wondering if there's another way.

r = np.random.randint(3, size=(100, 2000)) - 1

推荐答案

简单地给随机数生成器添加固定值,例如

Simply seed the random number generator with a fixed value, e.g.

numpy.random.seed(42)

这样,您将始终获得相同的随机数序列.

This way, you'll always get the same random number sequence.

这篇关于一致地创建相同的随机numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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