I have this code:
Random r = new Random();
while (mStack[step].hasNext()) {
int rand = r.nextInt(length);
for (int i = rand; i < length+rand; i++) {
//use of i and rand
}
}
and all this in a recursive call.
Will this seed a new Random for each while iteration, different for each recursive call?
Or I have to use
while (mStack[step].hasNext()) {
Random r = new Random();
int rand = r.nextInt(length);
for (int i = rand; i < length+rand; i++) {
//use of i and rand
}
}
Please advice
Constant re-seeding isn’t beneficial. Create a single instance of
Random, and pass it on the stack as a parameter to the recursive method.The no-arg
Randomconstructor in Java 6 uses the sum of a instance counter and the currentSystem.nanoTime()value as a seed. Of course, no re-seeding is performed bynextInt().