What is random seed about?

2020-06-27 02:25发布

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());
    }
}

标签: java random seed
6条回答
放荡不羁爱自由
2楼-- · 2020-06-27 02:43

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);
查看更多
Summer. ? 凉城
3楼-- · 2020-06-27 02:45

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

  Random rnd1 = new Random(System.currentTimeMillis())

for different outputs.

查看更多
一夜七次
4楼-- · 2020-06-27 02:48

42 is the seed, as the very same Javadoc says. So, what is a seed? A random number is seldom truly random - often it's a pseudo-random instead. 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!

However, depending on your needs, this pseudo-randomness may be enough - I said enough because generating random bit is expensive, and I'm not talking about time or memory, but about money (see this link on 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.

How can we describe a PRNG, like the one used in Math.random()? It's a function, initialized with a seed S 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):

                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.

查看更多
Fickle 薄情
5楼-- · 2020-06-27 02:48

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.

查看更多
兄弟一词,经得起流年.
6楼-- · 2020-06-27 02:52

From the Java documentation in the Random class

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);

So 42 is the seed given to the new Random() in your example

查看更多
The star\"
7楼-- · 2020-06-27 02:56

Random Seed on Wikipedia:

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.

查看更多
登录 后发表回答