python 与 random.seed() 一起使用的建议种子值是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1703012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:53:28  来源:igfitidea点击:

What is suggested seed value to use with random.seed()?

pythonrandom

提问by AJ.

Simple enough question:

足够简单的问题:

I'm using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this default to the current time, but this is not ideal. It seems like a string literal constant (similar to a password) would also not be ideal/strong

我正在使用 python random 模块生成随机整数。我想知道与 random.seed() 函数一起使用的建议值是多少?目前我让它默认为当前时间,但这并不理想。似乎字符串常量(类似于密码)也不理想/强

Suggestions?

建议?

Thanks, -aj

谢谢,-aj



UPDATE:

更新:

The reason I am generating random integers is for generation of test data. The numbers do not need to be reproducable.

我生成随机整数的原因是为了生成测试数据。这些数字不需要是可重复的。

回答by Jed Smith

According to the documentation for random.seed:

根据文档random.seed

If x is omitted or None, current system time is used; current system time is also used to initialize the generator when the module is first imported. If randomness sources are provided by the operating system, they are used instead of the system time (see the os.urandom()function for details on availability).

如果省略 x 或None,则使用当前系统时间;当前系统时间也用于在第一次导入模块时初始化生成器。如果操作系统提供随机源,则使用它们代替系统时间(os.urandom()有关可用性的详细信息,请参阅函数)。

If you don't pass something to seed, it will try to use operating-system provided randomness sources instead of the time, which is always a better bet. This saves you a bit of work, and is about as good as it's going to get. Regarding availability, the docs for os.urandomtell us:

如果您不将某些内容传递给种子,它将尝试使用操作系统提供的随机源而不是 time,这总是更好的选择。这可以为您节省一些工作,而且效果会非常好。关于可用性,文档os.urandom告诉我们:

On a UNIX-like system this will query /dev/urandom, and on Windows it will use CryptGenRandom.

在类 UNIX 系统上,这将查询 /dev/urandom,而在 Windows 上,它将使用 CryptGenRandom。

Cross-platform random seeds are the big win here; you can safely omit a seed and trust that it will be random enoughon almost every platform you'll use Python on. Even if Python falls back to the time, there's probably only a millisecond window (or less) to guess the seed. I don't think you'll run into any trouble using the current time anyway -- even then, it's only a fallback.

跨平台随机种子是这里的一大胜利;您可以放心地省略种子并相信它在您将使用 Python 的几乎每个平台上都足够随机。即使 Python 退回到时间,也可能只有一毫秒(或更少)的时间来猜测种子。我认为无论如何您都不会在使用当前时间时遇到任何问题——即使如此,这也只是一个后备。

回答by grokus

For most cases using current time is good enough. Occasionally you need to use a fixed number to generate pseudo random numbers for comparison purposes.

对于大多数情况,使用当前时间就足够了。有时您需要使用固定数字来生成伪随机数以进行比较。

回答by Mike Graham

Setting the seed is for repeatability, not security. If anything, you make the system lesssecure by having a fixed seed than one that is constantly changing.

设置种子是为了可重复性,而不是安全性。如果有的话,与不断变化的种子相比,使用固定种子会使系统不安全。

回答by John La Rooy

Perhaps it is not a problem in your case, but ont problem with using the system time as the seed is that someone who knows roughly when your system was started may be able to guess your seed (by trial) after seeing a few numbers from the sequence.
eg, don't use system time as the seed for your online poker game

也许在您的情况下这不是问题,但使用系统时间作为种子的一个问题是,大致知道您的系统何时启动的人可能能够在看到一些数字后(通过试验)猜测您的种子顺序。
例如,不要使用系统时间作为在线扑克游戏的种子

回答by Francesco

If you are using random for generating test data I would like to suggest that reproducibility can be important.

如果您使用随机生成测试数据,我想建议可重复性很重要。

Just think to an use case: for data set X you get some weird behaviour (eg crash). Turns out that data set X shows some feature that was not so apparent from the other data sets Y and Z and uncovers a bug which had escapend your test suites. Now knowing the seed is useful so that you can precisely reproduce the bug and you can fix it.

想想一个用例:对于数据集 X,你会得到一些奇怪的行为(例如崩溃)。事实证明,数据集 X 显示了一些在其他数据集 Y 和 Z 中不那么明显的特征,并发现了一个已经逃过测试套件的错误。现在知道种子很有用,这样您就可以精确地重现错误并修复它。