In what circumstances would you like to have another seed for a random number generator?

I am wondering under what circumstances would you like to have another seed for a random number generator. I see the code somewhere that people create a Random object every time it is needed, and also see that people sometimes have it as an instance variable that uses a single seed to construct the object. for instance

 // same seed class A { private Random random; public A() { random = new Random(); } public int nextInt() { return random.nextInt(10000); } public double nextDouble() { return random.nextDouble(); } } 

vs

 // different seed is used every time when you call the method class B { public int nextInt() { return new Random().nextInt(10000); } public double nextDouble() { return new Random().nextDouble(); } } 

Please ignore this project for these two wrapper classes, just trying to show the difference.

+5
source share
3 answers

Two reasons for reusing Random :

  • There is overhead for creating Random .

  • If the code runs fast, the creation of two different Random objects may end with the same initial value, i.e. The result will not be random. Even if the code is not so fast, different Random objects will be seeded with close sibling values, decreasing the randomness coefficient.

One of the reasons for having multiple instances of Random , although not the basis for creating a new one with every use. Quoting javadoc :

Java.util.Random instances are thread safe. However, the simultaneous use of the same instance of java.util.Random through streams can face competition and, as a result, low performance. Instead, use ThreadLocalRandom in multi-threaded constructs.

Even in a single-threaded application, using ThreadLocalRandom may be appropriate as an easy way to get a shared instance of Random .

+7
source

A pseudo-random number generator, such as java.util.Random , produces a deterministic sequence of numbers characterized by an initial seed. Such generators are created to create sequences that appear to vary randomly and (usually) uniformly over the entire PRNG range.

You get the same sequence of numbers if you start from the same semester. This may be useful in some circumstances, but it is usually undesirable. In particular, your numbers will not have any chance at all if you select PRNG with the same seed before generating each number.

But more subtle problems arise, even if you generate PRNG with different seeds before generating each number, including if you create a new Random instead of reusing the existing one. In this case, you are not guaranteed the same range or distribution of results, and your results may show correlations that they would not show if you sowed only once.

So when you say

I am wondering under what circumstances would you like to have another seed for a random number generator.

the only reason I can think of you wanting to change the seeds in one run of your program is that you have published enough of the PRNGs that you are worried about someone who determines its implementation and internal state, and therefore, the ability to predict subsequent numbers. In practice, this will only apply in special circumstances.

On the other hand, you always want to select a different seed every time your program starts, otherwise it will generate the same sequence of random numbers every time.

Typically, you should split the PRNG once, and then use it as much as necessary, without re-plating. It is not necessary to mistakenly use several independent PRNGs, but the reasons for this are mainly organizational in nature (for example, you do not need to worry about synchronization without having to specify a Random instance), but not functionally.

+6
source

If you really just need one value, only once, in the application, then any approach will give fairly similar results, and it does not matter. If you are going to pull out a lot of random numbers, in particular, the same algorithm that works repeatedly, you will get better (not faster, but better) results with reusing the same Random object.

The reason for having one or two objects is because you want to clearly distinguish between sequences of random numbers (which, apparently, you seeds in different ways and want to manage in different ways). It is up to the application to determine if it matters.

For most applications, you will need the default seed, but it can be very useful to give your application the ability to run with a predefined seed; eg:

  if (seedInputValueProvided) mRandom = new Random(seedInputValue); else mRandom = new Random(); 

What for? Testing. Development. If you create exactly one random object and reuse it with a known initial value, you can re-run certain scripts to re-test the same thing (or recreate the problems).

0
source

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


All Articles