I am wondering under what circumstances that you would want to have a different seed for you random number generator. I see code somewhere that people create a Random
object every time when needed, and also see people sometimes would have it as an instance variable that uses a single seed when the object is constructed. For example
// 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(); }
}
versus
// 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 design for these two wrapper classes, just trying to show the difference.
In other words, the sequence of random numbers that result from initializing the generator from seed = 42 should be just as random as the sequence that results from any other seed value. Personally, I use 12345 as my seed value. If I'm feeling particularly frisky, I might use 54321.
TL;DR; A seed usually enables you to reproduce the sequence of random numbers. In that sense they are not true random numbers but "pseudo random numbers", hence a PNR Generator (PNRG). These are a real help in real life!
What is a Random Seed? A random seed is a starting point in generating random numbers. A random seed specifies the start point when a computer generates a random number sequence. This can be any number, but it usually comes from seconds on a computer system's clock (Henkemans & Lee, 2001).
It's just down to the authors' choice. Further, if you are only ever setting the seed once in your code, then you can kind of choose any number you like.
Two reasons to re-use a Random
:
There is an overhead to creating a Random
.
If code is fast, creating two different Random
objects may end up using the same seed value, i.e. result will not be random. Even if code is not that fast, the different Random
objects will be seeded with closely related seed values, reducing the randomness factor.
One reason for having multiple Random
instances, though not a reason for creating a new one on every use. Quoting javadoc:
Instances of
java.util.Random
are threadsafe. However, the concurrent use of the samejava.util.Random
instance across threads may encounter contention and consequent poor performance. Consider instead usingThreadLocalRandom
in multithreaded designs.
Even in a single-threaded application, using ThreadLocalRandom
may be appropriate, as a simple means of obtaining a shared Random
instance.
A pseudorandom number generator, such as java.util.Random
, produces a deterministic sequence of numbers characterized by the initial seed. Such generators are crafted to produce sequences that appear to vary randomly and (usually) uniformly over the whole range of the PRNG.
You get the same sequence of numbers if you start with the same seed. That can be useful under some circumstances, but usually it is undesirable. In particular, your numbers will not have any appearance of randomness at all if you seed the PRNG with the same seed before generating each number.
But more subtle problems arise even if you seed the PRNG with different seeds before generating each number, including if you instantiate a new Random
instead of reusing an existing one. In this case, you are not guaranteed to have the same range or distribution of results, and your results may exhibit correlations that they would not exhibit if you seeded only once.
So, when you say
I am wondering under what circumstances that you would want to have a different seed for you random number generator.
, about the only reason I can think of for wanting to change seed within one run of your program is that you have published enough draws from the PRNG that you are concerned about someone determining its implementation and internal state, and therefore being able to predict subsequent numbers. This would be a concern in practice only under rather special circumstances.
On the other hand, you generally do want to choose a different seed each time your program runs, else it will generate the same sequence of random numbers each time.
Generally, you should seed your PRNG once, and then use it as much as needed, without reseeding. It's not necessarily wrong to use multiple, independent PRNGs, but the reasons for doing so are mostly organizational (e.g. not having to worry about synchronization, not having to provide for injecting a Random
instance) rather than functional.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With