What is the importance of using Random.setSeed?

When writing a Java program, we use setSeed in the Random class. Why should we use this method?

Is it possible to use Random without using setSeed ? What is the main purpose of using setSeed ?

+6
source share
5 answers

One of them is that it allows you to reproduce the results of your program in the future .

As an example, I wanted to calculate a random variable for each row in the database. I wanted the program to be reproducible, but I need randomness between the lines. To do this, I install a random number sample in the primary key of each row. Thus, when I ran the program again, I had the same results, but the random variable between the lines was pseudo-random.

+10
source

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

This can help.

A pseudo random 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, because it is completely determined by a relatively small set of initial values, called the PRNG state, which includes a truly random seed.

+3
source

I see two reasons for this:

  • You can create a reproducible random stream. For a given seed, the same results will be returned from consecutive calls of the (same) nextX methods.

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

  • For some reason, you feel that your seed is of a higher quality than the default source (which I assume is derived from the current time on your PC).

+3
source

A particular seed will always produce the same sequence of "random" numbers. Thus, in Random there are only 2 ^ 64 different sequences. In addition to setSeed you can also use a constructor with a seed. When not in use, a seed uses a computer clock to select a truly random seed.

Therefore, in the normal case, do not use a specific seed, the Random() constructor, and no setSeed . Especially refrain from setSeed(System.currentTimeMillis()) .

For a data-dependent debug where you want to repeat the same pseudo-random data, use a specific seed.

No need to worry about whether a particular seed / sequence and subsequent digits can be recognized due to the large number of seeds.

The default constructor uses System.nanoTime() (correctly distorted), which is an expensive OS function when it comes to thousands of new Random() .

+3
source

Some others mentioned reproducibility. Reproducibility is at the heart of debugging, you should be able to reproduce the circumstances in which the error occurred.

Another important use of reproducibility is that you can play some statistical games to reduce the variability of some ratings. See the Wikipedia Reduction article for more details, but the intuition is. Suppose you are considering two different layouts for a bank or grocery store. You cannot build them and see which one works better, so you use a simulation. From the theory of queues, you know that the line size and customer delays depend on the layout, and also partly due to changes in arrival times, demand loads, etc. Therefore, you use randomness in your two models. If you run the models completely independently and find that the lines are larger in layout 1 than in layout 2, it could be because of the layout, or maybe because in layout 1 there just appeared more clients or a more complex combination of transactions because good luck in a draw. However, if both systems use the same set of clients arriving at the same time and having the same transaction requirements, this is a β€œfairer” comparison. The differences that you observe are most likely due to the layout. You can do this by reproducing randomness in both systems - use the same seeds and synchronize so that the same random numbers are used for both purposes for the same purpose.

+1
source

Source: https://habr.com/ru/post/949093/


All Articles