2

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!

vnsakh
  • 21
  • 2
  • Have a look at the Java source code for `Random`. Copy it and add a `getSeed()` function. Call it `MyRandom` and use that version in code where you need to retrieve the seed. You may need to store the previous version of the seed to get the number you actually want; that is something you will need to check. – rossum May 01 '20 at 14:13
  • `Random` is `Serializable`. I ***think*** you can just write it somewhere at the 9,999th iteration and then continue generating values. When you want to return to that previous state, simply de-serialize the previously saved state. Note that you could also `new Random(1);` and re-create all 9999 values again. – Elliott Frisch May 01 '20 at 14:20
  • I just iterated over 100_000_000 random numbers in less than 5 seconds. Assuming I understand your requirement, It seems to me that you can save each seed that you create. Once you determine the seed that works best, you can iterate up to the number of that random instance and restart the processing. – WJS May 01 '20 at 14:25

0 Answers0