Possible Duplicate:
Java random always returns the same number when I set the seed?
I’m using the Java’s Random class and the nextInt() method to get random numbers. But it seems like the numbers are always in the same order. Is there a way to fix this? I know some random generators take in a seed value, then you use the system timer for the seed.
Code:
This is what you really need to refer to https://stackoverflow.com/a/11705615/1143977. Quoting my answer from the reference link.
There are two issues causing what you see. The first is that the code sets a seed value for a Random instance. The second is that the instance method “random” instances a new Random object and then immediately sets its seed with the same seed every time. The combination of these two guarantees that, for the same value of i, the method “random” will always return the same value and it will always be the first in the sequence that the seed always generates.
Assuming setting the seed is mandatory, to get the next value in the sequence instead of the same first value of the sequence every time, the randnum instance of Random can’t have its seed set every time just before its next method gets called. To fix that, move the randnum local variable instance of Random from the scope of the random instance method to the class scope. Second, set the seed only when random is assigned a Random instance or only to get same sequence of results from it to start over again. Class Random’s setSeed(long seed) instance method can’t execute in the class scope, so the constructor has to set it using the Random constructor with the long seed parameter. The following code shows the changes:
The above will give you an exact solution to your exact problem, as stated. However, using a mandatory seed seems unusual, given what it does.
The following explains more about Random, seeds for Random and why there is a provision for supplying a seed.
Random has two constructors:
and
and an instance method
that all affect the sequence of numbers obtained from a Random instance. The instance method,
sets the Random object to the same state it would have been in had it been just instanced with the same seed as the constructor argument. Only the low-order 48 bits of a seed value get used.
If a Random object is instanced without a seed, the seed will be the same as the system time in milliseconds. This ensures that, unless two Random objects are instanced in the same millisecond, they will produce different pseudo-random sequences. Only the low order 48 bits of the seed value gets used. This causes unpredictable pseudo-random sequences. It is not necessary and wasteful of computing resources to get a new instance of Random every time one calls a next method.
Random’s seed parameters are provided so that one may instance a Random object that produces a sequence that is repeatable. For a given seed, the sequence of values in next methods are guaranteed to be the same sequence whenever that seed is used. This is useful for testing software that is going to use pseudo-random sequences where results have to be predicable and repeatable. It is not useful for creating different unpredictable pseudo-random sequences in operation.