简体   繁体   English

在 Python 中运行 numpy.random.randint() 后冻结输出

[英]Freezing output after running numpy.random.randint() in Python

I've used numpy.random.randint(lower limit, upper limit, size) to make a 2D array with random numbers within the given range.我使用 numpy.random.randint(lower limit, upper limit, size) 在给定范围内使用随机数制作二维数组。 Now I want to freeze this randomly generated array for the follow up steps.现在我想冻结这个随机生成的数组以用于后续步骤。 So that the numbers don't change every time I run the entire script.这样每次运行整个脚本时数字都不会改变。 Is there a way to do this?有没有办法做到这一点?

Thanks.谢谢。

Set a seed so that the random numbers generated are same every time you run it.设置一个种子,以便每次运行时生成的随机数都相同。

numpy.random.seed(0)

Docs 文档

By seeding random by hand you can get the same random number whenever you call it.通过手动random播种,无论何时调用它,您都可以获得相同的随机数。 You can seed the random() function using seed() function.您可以使用seed()函数为random()函数设定seed() The input is the seed and the same seed input will result the same output.输入是种子,相同的种子输入将产生相同的输出。

from numpy import random

random.seed(1)
first = random.randint(10)

random.seed(1)
second = random.randint(10)

In this code both first and second will be same.在此代码中, firstsecond将相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM