java 使用 Random.setSeed 的重要性是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17565447/
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-01 02:20:44  来源:igfitidea点击:

What's the importance of using Random.setSeed?

javarandom

提问by nilashan

When writing Java program, we use setSeedin the Randomclass. Why would we use this method?

在编写Java程序时,我们setSeedRandom类中使用。我们为什么要使用这种方法?

Can't we just use Randomwithout using setSeed? What is the main purpose of using setSeed?

我们Random不能不使用就使用setSeed吗?使用的主要目的是setSeed什么?

回答by Owen

One use of this is that it enables you to reproduce the results of your program in future.

这样做的一个用途是它使您能够在将来重现程序的结果

As an example, I wanted to compute a random variable for each row in a database. I wanted the program to be reproducible, but I wanted randomness between rows. To do this, I set the random number seed to the primary key of each row. That way, when I ran the program again, I got the same results, but between rows, the random variable was pseudo random.

例如,我想为数据库中的每一行计算一个随机变量。我希望程序可重现,但我希望行之间具有随机性。为此,我将随机数种子设置为每一行的主键。这样,当我再次运行程序时,我得到了相同的结果,但是在行之间,随机变量是伪随机的。

回答by NINCOMPOOP

The seed is used to initialize the random number generator. A seed is used to set the starting point for generating a series of random numbers. The seed sets the generator to a random starting point. A unique seed returns a unique random number sequence.

种子用于初始化随机数生成器。种子用于设置生成一系列随机数的起点。种子将生成器设置为随机起点。唯一的种子返回唯一的随机数序列。

Thismight be of help .

可能会有所帮助。

A pseudorandom number generator (PRNG), also known as a deterministic random bit generator DRBG, is an algorithm for generating a sequence of numbers that approximates the properties of random numbers. The sequence is not truly random in that it is completely determined by a relatively small set of initial values, called the PRNG's state, which includes a truly random seed.

伪随机数生成器 (PRNG),也称为确定性随机位生成器 DRBG,是一种用于生成近似随机数特性的数字序列的算法。该序列不是真正随机的,因为它完全由一组相对较小的初始值确定,称为 PRNG 的状态,其中包括一个真正随机的种子。

回答by Duncan Jones

I can see two reasons for doing this:

我可以看到这样做的两个原因:

  1. You can create a reproducible random stream. For a given seed, the same results will be returned from consecutive calls to (the same) nextXmethods.

    If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers

  2. You feel, for some reason, that your seed is of a higher quality than the default source (which I'm guessing is derived from the current time on your PC).

  1. 您可以创建可重现的随机流。对于给定的种子,连续调用(相同)nextX方法将返回相同的结果。

    如果使用相同的种子创建 Random 的两个实例,并且对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列

  2. 出于某种原因,您觉得您的种子质量高于默认来源(我猜这是从您 PC 上的当前时间得出的)。

回答by Joop Eggen

A specific seed will always give the same sequence of "pseudo-random" numbers. So there are only 2^48 different sequences in Randombecause setSeedonly uses 48-bits of the seedparameter! Besides setSeed, one may also use a constructor with a seed (e.g. new Random(seed)).

特定的种子将始终给出相同的“伪随机”数字序列。所以只有 2^48 个不同的序列,Random因为setSeed只使用了seed参数的48 位!此外setSeed,还可以使用带有种子的构造函数(例如new Random(seed))。

When setSeed(seed)or new Random(seed)are notused, the Random()constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor.

setSeed(seed)或者new Random(seed)使用的,Random()构造函数将随机数发生器的种子来的值很可能是从该构造的任何其它调用不同。

Java reference for the above information: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Random.html

以上信息的Java参考:https: //docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Random.html

In the ordinary case, don't use a seed. Just use the empty constructor Random()and don'tcall setSeed. This way you'll likely get different pseudo-random numbers each time the class is constructed and invoked.

在一般情况下,不要使用种子。只需使用空构造函数Random()不要调用setSeed. 这样,每次构造和调用类时,您可能会得到不同的伪随机数。

For data dependent debugging, where you want to repeat the same pseudo-random numbers, use a specific seed. In this case, use Random(seed)or setSeed(seed).

对于数据相关的调试,您想要重复相同的伪随机数,请使用特定的种子。在这种情况下,请使用Random(seed)setSeed(seed)

For non-security critical uses, there's no need to worry whether the specific seed/sequence might be recognized and subsequent numbers predicted, because of the large range of seeds. However, "Instances of java.util.Random are not cryptographically secure. Consider instead using SecureRandomto get a cryptographically secure pseudo-random number generator for use by security-sensitive applications." source

对于非安全关键用途,由于种子的范围很大,因此无需担心是否可以识别特定种子/序列并预测后续数字。但是,“java.util.Random 的实例在加密方面并不安全。请考虑使用SecureRandom以获取加密安全的伪随机数生成器,以供对安全敏感的应用程序使用。” 来源

回答by pjs

Several others have mentioned reproducibility. Reproducibility is at the heart of debugging, you need to be able to reproduce the circumstances in which the bug occurred.

其他几个人提到了可重复性。可重现性是调试的核心,您需要能够重现错误发生的情况。

Another important use of reproducibility is that you can play some statistical games to reduce the variability of some estimates. See Wikipedia's Variance Reduction articlefor more details, but the intuition is as follows. Suppose you're considering two different layouts for a bank or a grocery store. You can't build them both and see which works better, so you use simulation. You know from queueing theory that the size of lines and delays customers experience are partly due to the layout, but also partly due to the variation in arrival times, demand loads, etc, so you use randomness in your two models. If you run the models completely independently, and find that the lines are bigger in layout 1 than in layout 2, it might be because of the layout or it might be because layout 1 just happened to get more customers or a more demanding mix of transactions due to the luck of the draw. However, if both systems use the exact same set of customers arriving at the same times and having the same transaction demands, it's a "fairer" comparison. The differences you observe are more likely to be because of the layout. You can accomplish this by reproducing the randomness in both systems - use the same seeds, and synchronize so that the same random numbers are used for the same purpose in both systems.

再现性的另一个重要用途是您可以玩一些统计游戏来减少某些估计的可变性。请参阅维基百科的方差减少文章更多细节,但直觉如下。假设您正在为银行或杂货店考虑两种不同的布局。您无法同时构建它们并查看哪个效果更好,因此您使用模拟。您从排队理论中知道,线路的大小和客户体验的延迟部分是由于布局,部分是由于到达时间、需求负载等的变化,因此您在两个模型中使用随机性。如果您完全独立地运行模型,并发现布局 1 中的行比布局 2 中的行大,则可能是因为布局的原因,也可能是因为布局 1 恰好获得了更多客户或要求更高的交易组合由于抽签的运气。然而,如果两个系统使用完全相同的一组同时到达并具有相同交易需求的客户,则这是一个“更公平”的比较。您观察到的差异更有可能是由于布局。您可以通过在两个系统中重现随机性来实现这一点 - 使用相同的种子并进行同步,以便在两个系统中将相同的随机数用于相同的目的。