Saving and restoring the RNG state?

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Saving and restoring the RNG state?

      Hello, Mike. First off, thanks for a great book.

      I'm using the RNG provided in GCC2 in my game engine and it works fine...but now I'm putting in multiplayer and I need to be able to save the state of the RNG and restore it. You actually mention in the book that the source code includes this ability, but I don't see it in the actual source files.

      Saving the current seed and the current index into the table doesn't work, because the initial numbers are quickly generated by a simple "multiply and mask" method, and then the numbers after that are twisted. I supposed that I could keep a cumulative count of how many numbers have been generated since the birth of the object and then restore it by reseeding with the same seed and generating that many numbers again, but that seems kind of hacky.

      If you know of a better way, I'd love to hear it. Again, thank you for the book, it's been incredibly helpful to me.
      "It's often uncomfortable, but in the long run it makes more sense to deal with reality."
      - Michael Abrash
    • RE: Saving and restoring the RNG state?

      What exactly are you trying to accomplish? If your goal is to generate the same series of random numbers (to create a play-back for example), all you should need to do is save the initial seed when it's generated.

      If you're trying to save the current state of the random seed, you can call CRandom::GetRandomSeed() and save that, but I'm not really sure what that would accomplish. You won't know what the next series of numbers will be so you might as well reseed it normally.

      -Rez
    • The problem with seeding once at the beginning is, what happens when you save and reload a game state? If you save a game state, quit the game and then start it back up again and then use the same seed, you'll be at a different point in the number walk and thus you'll get different results.

      The reason I'm doing this is because I'm trying to write an RTS that is both multiplayer-enabled and can save out replays of gameplay that always play back the same way.

      I actually got what I wanted by saving the initial seed and the number of times the RNG has generated new numbers; on load I reseed and then quickly generate the same number of numbers, which restores the RNG's state.
      "It's often uncomfortable, but in the long run it makes more sense to deal with reality."
      - Michael Abrash