vendredi 17 janvier 2020

Remember location(?) in Random seed?

First off: I'm mostly self-taught, so please forgive me if I don't know the correct terminology or make some rookie mistake.

So, I am making a Random variable with a seed. When you're getting the "next" from the variable, it gets the next value from that seed. Is there any way to record your location in a random variable? For example, ideally the following code would output two identical strings:

//My attempt to save my location in a Random:

r = new Random(5);

for (int i = 0; i < 10; i++) {
    System.out.print(r.nextInt(2));
    if (i == 9) {
        System.out.print(" ");
    }
}

//====================================

System.out.println("\n-------------");
r = new Random(5);
for (int i = 0; i < 5; i++) {
    System.out.print(r.nextInt(2));
}

r = new Random(r.nextLong());
System.out.print(" ");
for (int i = 0; i < 5; i++) {
    System.out.print(r.nextInt(2));
}

but instead returns:

1001010100 1100110011
-------------
1001010100 0010010111
-------------

Is there some way to "remember" my location, such that I can create a new Random variable that would continue where the previous one left off? Preferably, I'd rather not serialize the variable - it wouldn't be the end of the world, but I'm wondering if there's some other way.




Aucun commentaire:

Enregistrer un commentaire