Role of seed in random number generation

2019-01-20 19:08发布

I have a big question in my mind:

I can use a seed number to generate random numbers:

Random rand = new Random(34666666);

But the thing that I cannot understand is the role of that seed. For example what is the difference of

That code with the following:

Random rand = new Random();

标签: java random
5条回答
2楼-- · 2019-01-20 19:48

I would suggest reading the Java Documentation on Random. http://docs.oracle.com/javase/7/docs/api/java/util/Random.html

Some important bits:

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.

public Random()
Creates a new random number generator. This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor.
查看更多
forever°为你锁心
3楼-- · 2019-01-20 19:50

When you supply a specific, hard-coded seed, to the one-arg Random constructor, the random numbers that will be generated will always be the same, every time you run the program. That is needed when you need a predictable source of random numbers.

However, when you don't supply a seed, then the Random constructor will choose a seed for you, based on System.nanoTime. The random numbers will be different every time you run the program, because the seed will be different each time.

Creates a new random number generator. This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor.

This is important because Java's random number generator is pseudo-random; each new pseudo-random number affects the seed that is used for the next pseudo-random number that gets generated.

查看更多
趁早两清
4楼-- · 2019-01-20 20:02

The Java random function is this:

numberi+1 = (a * numberi + c) mod m

The number is your seed.

The random is not really a random number, if I know your current number then I can know all of the numbers you'll get in the future. (this is why you should always use a static random and never create a new random (you will get the same number))

If you need a more secure random, use the SecureRandom class of java.

It provides you with a more secure random that uses stuff like, number of movements the mouse made, the current location of the mouse, number of seconds since to last typed and so on...

查看更多
你好瞎i
5楼-- · 2019-01-20 20:03

The seed allows you to get a determined sequence of random numbers.

It is especially useful when you need to generate random numbers but also be able to launch again and obtain the same results.

Without a seed, the constructor chooses one for you depending on the time when the Random object is instanciated.

查看更多
趁早两清
6楼-- · 2019-01-20 20:03

If you take a look at the code for java.util.java (http://developer.classpath.org/doc/java/util/Random-source.html), you will see that the constructor uses the current timestamp for its seed:

public Random()
{
  this(System.currentTimeMillis());
}
查看更多
登录 后发表回答