Java 随机种子是关于什么的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23127392/
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
What is random seed about?
提问by newbieprogrammer
For example the code below. It has a random class. However it always produce the same output everywhere . In this case which item is the seed?
例如下面的代码。它有一个随机类。然而,它总是在任何地方产生相同的输出。在这种情况下,哪个项目是种子?
source: link
来源:链接
import java.util.Random;
public class RandomTest {
public static void main(String[] s) {
Random rnd1 = new Random(42);
Random rnd2 = new Random(42);
System.out.println(rnd1.nextInt(100)+" - "+rnd2.nextInt(100));
System.out.println(rnd1.nextInt()+" - "+rnd2.nextInt());
System.out.println(rnd1.nextDouble()+" - "+rnd2.nextDouble());
System.out.println(rnd1.nextLong()+" - "+rnd2.nextLong());
}
}
回答by Codor
The seed is given as the argument of the constructor of Random
; using the same seed will yield the same sequence of numbers. However this is discussed under the link in thet question.
种子作为 的构造函数的参数给出Random
;使用相同的种子将产生相同的数字序列。但是,这是在问题中的链接下讨论的。
回答by Nambi
From the Java documentation in the Random class
来自Random 类中的 Java 文档
Creates a new random number generator using a single long seed. The seed is the initial value of the internal state of the pseudorandom number generator which is maintained by method next(int).
The invocation new Random(seed) is equivalent to:
Random rnd = new Random(); rnd.setSeed(seed);
使用单个长种子创建一个新的随机数生成器。种子是伪随机数生成器内部状态的初始值,由方法next(int)维护。
调用 new Random(seed) 相当于:
随机 rnd = 新随机();rnd.setSeed(种子);
So 42 is the seed given to the new Random()
in your example
所以 42 是给new Random()
你的例子中的种子
回答by niiraj874u
The seed is the initial value of the internal state of the pseudorandom number generator which is maintained by method next(int).
种子是伪随机数生成器内部状态的初始值,由方法 next(int) 维护。
The invocation new Random(seed) is equivalent to:
调用 new Random(seed) 相当于:
Random rnd = new Random();
rnd.setSeed(seed);
回答by Luke Vo
A random seed (or seed state, or just seed) is a number (or vector) used to initialize a pseudorandom number generator.
随机种子(或种子状态,或只是种子)是用于初始化伪随机数生成器的数字(或向量)。
In other word, it is the number from which a seem-to-be-random sequence will be generated. Therefore, if you use the same number, the senquence will always be the same.
换句话说,它是生成一个看似随机的序列的数字。因此,如果您使用相同的数字,则序列将始终相同。
In practice, we usually use System Time as seed.
在实践中,我们通常使用 System Time 作为种子。
回答by PetrS
In this case the seed is 42. This is the reason for the same output - you use the same seed. You can use for example
在这种情况下,种子是 42。这就是输出相同的原因 - 您使用相同的种子。您可以使用例如
Random rnd1 = new Random(System.currentTimeMillis())
for different outputs.
对于不同的输出。
回答by Raffaele
42 is the seed, as the very same Javadocsays. So, what is a seed? A randomnumber is seldom trulyrandom - often it's a pseudo-randominstead. This means it's generated from a function, which is said PRNG (pseudo random number genrator). Being generated from a function, in turn, means that the output is not random anymore, since it's predictable!
42 是种子,正如Javadoc所说的那样。那么,什么是种子?一个随机数是很少真正随机的-通常这是一个伪随机代替。这意味着它是由一个函数生成的,即所谓的 PRNG(伪随机数生成器)。反过来,从函数生成意味着输出不再是随机的,因为它是可预测的!
However, depending on your needs, this pseudo-randomness may be enough - I said enoughbecause generating random bit is expensive, and I'm not talking about time or memory, but about money (see this linkon wikipedia). So, for example, if you need a random value to place enemies in your game, a pseudo-random number is ok - but if your are building security-related software, you want to use a true random number, or at least a cryptographically secure PRNG.
但是,根据您的需要,这种伪随机性可能就足够了 - 我说得够多了,因为生成随机位很昂贵,我不是在谈论时间或内存,而是在谈论金钱(请参阅维基百科上的此链接)。因此,例如,如果您需要一个随机值来在您的游戏中放置敌人,那么伪随机数就可以了——但是如果您正在构建与安全相关的软件,您想要使用一个真正的随机数,或者至少是一个加密的安全的 PRNG。
How can we describe a PRNG, like the one used in Math.random()
? It's a function, initialized with a seedS that returns an array of values A. Note that, for each integer S, is defined one and only one array A. For example (values are not actual):
我们如何描述一个 PRNG,就像 中使用的那样Math.random()
?这是一个函数,用种子S初始化,返回值 A 的数组。请注意,对于每个整数 S,定义一个且只有一个数组 A。例如(值不是实际的):
first call second call third call
seed: 14329 .18 .82 .5
seed: 3989 .7 .02 .93
So you seed you PRNG with some known value when you want its result to be predictable - for example for testing purposes or to ensure that, each time you run level 1 in your game, the enemies are always placed in the same (pseudo) random places - otherwise you don't need to explicitely pass a seed.
因此,当您希望其结果是可预测的时,您可以为 PRNG 设置一些已知值 - 例如出于测试目的或确保每次在游戏中运行级别 1 时,敌人始终放置在相同的(伪)随机中地方 - 否则你不需要明确地传递种子。