# Reversing a procedural generation

This topic is 1406 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Procedurally generated data has a random seed and random values generated by the process. Here is an example of what I mean:

You have a row of windows you want to either be broken or unbroken. You set you seed and loop through the "windows" randomly assigning "broken" or "unbroken" to each according to the output of the random number generator.

This is simple and straight forward......

What if I wanted to do that in reverse? If I started with all the windows "unbroken" and I throw rocks randomly and broke a few of them. I would like to reverse the process until I get THE random seed that would result in generating the windows into that state.

I'm sure it CAN be done, but how?

This process could be very useful in games that have a huge amount of "object states". Like a planet with mineral deposits. If the player collects a certain amount of them, it would be silly for the player to be able to come back to the planet and get those deposits again. And saving the state of each mineral deposit on every planet in every solar system would be crazy!

##### Share on other sites

If I have a random seed of 12345 and with the process, the windows generate:

1,0,1,1,0,0,0,0

where 1=="broken" and 0=="unbroken"

Then every time I go to generate the windows with seed 12345 it will look exactly the same.

If I break a window:

1,0,1,1,0,1,0,0 then the seed 12345 won't generate this result.

There IS some seed that will generate 1,0,1,1,0,1,0,0..... I want to go from the result to the seed. It's not magic, it's logic I am not fully aware of.

##### Share on other sites

This is a SUPER SIMPLE random number generator:

int RNSeed=12345;
int randomnum(void){
RNSeed=abs(RNSeed*8786545643)%987654321;
return RNSeed;
}



With this initial seed, the first time it's called it results in 770159010, and the second time it results in 808599291.

This will happen EVERY time I start with 12345. The problem with reversing the calculation is the modulus of 987654321. Any seed value greater than that number will be some value that can't be found and therefore the previous seed us unknown. The solution would involve a random number generator that can be reversed-- I don't know how to make that (or if it can be made at all).

All of you are either think I don't know how random generators work or you are missing my point entirely.

##### Share on other sites

You have 2 windows and a 1 bit seed. 0 is both windows broken, 1 is both windows intact. You want a seed that leads to only 1 broken window... there isn't one. Now think larger scale and you'll understand why you can't do what you want to do.

##### Share on other sites
You can design a PRNG that is easily reversible, by using just about any private-key cryptographic method (any block cipher would do).

Or you can just perform a sequence of reversible operations on sets of 64 bits, like
* multiply by an odd constant
* XOR with a constant
* rotate the bits (e.g., `seed = (seed << 24) ^ (seed >> 40);')

A long enough sequence of these operations will hash the seed into being completely unrecognizable, and that can be used as a PRNG. The inverses of these operations are relatively straight-forward, and that allows you to reverse the process and compute the seed from a result.

However, I think we all understood your situation perfectly well and you should re-read what we posted.

##### Share on other sites

You can design a PRNG that is easily reversible, by using just about any private-key cryptographic method (any block cipher would do).

Now that's positive thinking!

I thank you all for responding. After serious consideration, I think you may be right about one thing: the random number generator wouldn't have calculations that would repeat numbers. This means even if I had some linear values, I couldn't have all 0's or all 1's or any number. Otherwise it wouldn't be a PRNG.... The game state should have the ability to have any valid state for all parameters.

+1 to all......

##### Share on other sites

Generate random locations for all mineral deposits every time you visit a planet.

When a player visits a planet, typically they will land at a location and collect all the minerals within range of their ship.

So work out a region that has been mined out. Store the region in the save game.

Next time you come to the planet, generate all the minerals again and compare each one to the mined region.

When you leave the planet, rationalise the region list. I.E. if two regions are contiguous, merge them into a single region.

Eventually you will end up with a single region, the whole planet.

The save game storage would not be a major problem then, and a simple point / region intersection test will not slow down the game very much.

##### Share on other sites

Yet another example is MineCraft. The world is generated by a 32-bit seed, yet it is infinitely large (theoretically). Obviously, you can't reverse that back into a seed, as explained above.

I would like to point out that early versions of MineCraft used to save the differences between the original world generated by the seed and the modified world edited by the player. Alas, the longer the world existed and the more you mined, the larger the save-files became and the longer it took to apply the differences whenever you loaded the world.

I recommend saving absolute states and not relative states. You can optimize it in many ways, one way is only saving the sub-sections that are different than what was generated by the seed.

##### Share on other sites

At this point it's probably obvious - there is no encoding that will work like you want it to

That's correct.....

To everyone: I've moved away from the idea of using a seed as a save state.

Perhaps I should start a new post on saving the state of a galactic sized game.....

Any ideas on minimizing the save game size for really huge games?

##### Share on other sites

If a certain amount of time has passed since anyone visited a given planet/system/galaxy, you can probably prune the save data for it from the save file. Hand-wave it away as enough time has passed that the things that were done have been erased by natural processes. This pruning can be done whenever the save file is accessed to keep it lean.

Compress as much as you can. If something is either there or not there, figure out some way of storing it as a single bit if possible, rather than as, say, a bool

Use a binary format rather than a text-based one. Text formats are for human-readable content and can vastly inflate file size.

If you are doing voxel world generation, look into storage schemes for large amounts of array data, such as run-length encoding schemes used for TGA files. Research lossless image compression to get ideas for how you can compress your array data.

##### Share on other sites

That sounds interesting. How about storing save data as data for an image like .png? The data would look like a picture (noise), but it would utilize the image's compression..... Don't know if that's what you were getting to, but it made me think of it anyway.

Some values will be actual bites and some will be stored/read as bites but actually have 8 yes/no values..... Sounds like a good way to encrypt it too-- don't know if anyone is using this method.

##### Share on other sites

How about storing save data as data for an image like .png? The data would look like a picture (noise), but it would utilize the image's compression..... Don't know if that's what you were getting to, but it made me think of it anyway.

Yeah, that might make sense if you were using a library that had png support and for some reason weren't able to use some other 3rd party compression library.

I don't know the details of png compression, but I wouldn't be surprised if it made some assumptions about what a typical image looks like (e.g. smooth gradients from pixel to pixel, etc...). Basically, it assumes the data is an image, and will probably compress that better than arbitrary data.

The point being, if you know something about your data, you can probably choose a compression scheme that will work better with it.

##### Share on other sites

I don't know a lot about data compression. I've never written anything that used it. Image files are compressed on the disk (except .bmp). I was simply implying I could use something that already had support through DirectX.

##### Share on other sites
You can also use a more straight-forward compression library, without the hack of going through an image. Start with something like zlib.

##### Share on other sites

You certainly can store your data in an image format, and may see some benefit from that format's compression -- because image formats are designed with images in mind, your non-image data may not get an optimal benefit from that compression -- you could get better compression with something designed specifically for the type of data you're storing.

##### Share on other sites

Voxel world chunks do tend to end up very image-like, though, enough so that schemes such as RLE can achieve pretty significant compression. The caveat, of course, is that the more types of blocks you include, and the more widespread and scattered the types are throughout the world, the less effective RLE will be. You wouldn't want to actually use an image compression library (they're optimized for the 2D case, usually) but you can look to them for ideas.

##### Share on other sites

Álvaro, the zlib link takes me to the first post in this thread