vendredi 1 mai 2020

Java Random. Is it possible to define the seed which correspond to the current state of a Random-instance?

I have a Random instance in my code which I initialize once at the beginning with the seed 1:

Random random = new Random(1);

This instance is used repeatedly in the code to get random integers:

int cutPoint1 = random.nextInt(maxIndexValue - minIndexValue + 1);

int cutPoint2 = random.nextInt(maxIndexValue - minIndexValue + 1);

This means that if I ask for a new int, it will be calculated according to the last state of this Random instance. I do not set the seed again so that I get different integers because it does not make sense for my program to have always the same cut points. I have millions of iterations, and I would like to reproduce one definite iteration, for example, the 1000th one. The problem is that the Random-instance has already changed its state. Is it possible to get the state of this random instance and some how get a seed which corresponds to this exact state? So that I can use it to reproduce the iteration?

Thank you in advance!




Aucun commentaire:

Enregistrer un commentaire