How do I resume a seeded random list of values at a specified step?

0 favourites
  • 9 posts
From the Asset Store
Template for scrollable list, fully documented in comment and video
  • I'm making a minesweeper clone where levels are of infinite size and generated as you progress, and I'd like to implement "seeded" randomness so that you can, for example, compete against others with the same exact grid.

    I've figured out how to implement custom seeds, but when saving then loading the custom seed the AdvancedRandom object's random sequence of numbers will "start over" instead of resuming at the seed's step where you saved and quit.

    How can I resume at the same point I saved at within my sequence of seeded random values?

    My current solution works like so, but this is inelegant:

    1) Player loads up their game

    2) Game loads in the grid with player's progress

    3) Repeat [n] times: Set someVariable to random(0,1)

    4) Player continues at the point where they left off

    #3 is the inelegant part... variable [n] in #3 is the number of times I've had AdvancedRandom give me a random value since starting the save file. Looping "output random value" n times puts us back at the step where the player left off.

    Thanks for any help you can give me here!

  • I'm not sure I understand the problem, but I still have a few ideas :)

    Before saving, you can generate a new seed string. When loading, set seed to that saved string.

    Or you can probably generate and apply a new seed on every step. So when a game is loaded at step 155, for example, you can tell Advanced Random to use seed "seed_step_155".

    Or maybe you don't need random to be seeded after loading the saved game? In this case, simply disable it.

  • Ok so let's simplify the problem. When using seeded randomness I am guaranteed to always get the same numbers in the same order, if I use the same seed. So let's say I do the following:

    Start of Frame:

    - Set AdvancedRandom Seed to "SOMESEED"

    Repeat 10 times:

    - Set Var to Floor(Random(0,10))

    - Set Text to Text.Text & " " & Var

    AdvancedRandom has now generated 10 random numbers. The above code will output the following, for example:

    1 5 7 6 3 8 3 2 4 6

    Now, let's say at this point I want to save my game and come back later. So I save the seed "SOMESEED", I save the string of output numbers (in bold above), and I quit. When I load up this save file, and generate 10 more numbers using the same string, my string will be:

    1 5 7 6 3 8 3 2 4 6 1 5 7 6 3 8 3 2 4 6

    Which is just the 1st 10 numbers twice.

    If I do all of the above without saving, quitting, and loading the save file, I'll have 20 random numbers. The intended functionality would allow me to save, quit, and load while resuming at the "step" that AdvancedRandom left off at. But there's no way to do that that I can find.

    Basically, I'd like a player that uses "SOMESEED" and plays for 20 steps to get the same results as a player who plays for 10 steps, then quits, then resumes and plays for 10 more steps.

    Does that make sense?

  • The easiest solution is to pre-generate numbers for all steps, put them into an array. When you need a random number for the next step, just get it from the array.

    You can also try what I suggested earlier - setting a new seed before each step. The entire sequence will still remain seeded. And when you load the game, you set the seed for the current step and continue.

  • So pre-generating numbers is great but what if I need like 8,000,000,000 of them? Since the game generates as you progress, I can't be sure how far players will make it. Granted, 8 billion will most definitely be enough, but yeah.

    Now that I've thought about it, re-seeding each step is an idea, I'll look into it. Does Advanced Random have a seed string maximum length? Because like I mentioned the game could potentially get into the millions of steps.

  • the game could potentially get into the millions of steps.

    But the string for the millionth step will still be only 7 characters long :)

    You can build the seed string like this: SeedPrefix&"_"&Step

    Where SeedPrefix is a truly random number you generate at the start of the game.

  • Well... More than 7 characters.

    "MYSEED_1234567" is 14 characters, hence why I ask. I suppose I can test myself and figure it out :P

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Length-wise it should be fine, but test the performance. I don't know how seeded random is implemented in C3, but I've seen small lags when changing seed too often in Construct 2 (using an addon).

  • Alright so I've learned a few things:

    1) AdvancedRandom can take a string of presumably any length - I used Update Seed and put in a string with over 1,000 characters and it worked as expected.

    2) Repeating the random() function 1,000,000 times BARELY noticeably lags, maybe 1/20 of a second.

    3) Updating the seed each time I use random() to "MYSEED_[n]" has no noticeable effect on FPS, even if calling it 100 times every single tick. (n = number of times I've used random())

    For both 2 and 3 I enabled "Replace system random" in the AdvancedRandom properties.

    So it looks like my current implementation will work fine (unless the player has been playing on a single save file for literally days without a game over). And so will your idea of updating to MYSEED_[n] each time random() is used :)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)