Seeding random numbers to store a randomly-generated video game level
This is just something I'm wondering about, after playing a particular video game on my iPhone.
In short, the game has only one level, but randomizes it each time (this is true of a number of games I imagine). So think of it as any kind of racing/running/whatever game, and as you go through it, unlike, say, one of the Mario games, the world is different around you each time so it's always new, even though there's only "one level" or just one environment.
It's fairly clear what the variables are-- every 30 seconds or so the background/environment changes to something new; obstacles appear in random places (and in harder places as you get farther in the level); extra characters run around in random places; and extra power-ups and so forth can be found at random times, with random values.
Easy enough. It's just set to random, basically a complicated version of any dice-rolling or card-shuffling game.
But it's not quite that simple. What I noticed the other day was that if I clicked "restart" instead of dying and exiting or just quitting the game, then I would restart the exact same level [actually, I'm not 100% certain that nothing is still variable, but most relevant things are exactly the same].
So there are technically two ways to do this:
1. Somehow store the level as a whole, already generated. This is impossible, because there are infinitely many variations that might occur (it's never exactly the same thing twice). It's remotely possible that it is stored throughout each race so that you can restart it and it remembers all of the randomly generated values, but that seems like a waste of energy on the part of the programmer and too much memory to use.
Therefore, I think it's much more likely that it's option 2:
2. It is generated "randomly", but not truly randomly. Instead, I think every race begins with generating a random seed number (perhaps from the time of day, milliseconds, or whatever is easiest). Then this value is stored. And everything is generated from it.
So what I want to ask is the following: would that work?
Is it possible to store a single seed number (randomly generated itself) and then exactly replicate an otherwise completely random level? That seems really interesting to me.
Likewise, you could use this for a website-- randomly show several ads, several embedded blog posts, and a few images of the day. Every time you reload the page you get something new. But, if you happen to do something (use some special URL, store a cookie, whatever) you could store a single seed number that would then generate exactly the same thing.
In other words, but using a non-random, seeded random algorithm, you can actually store a huge amount of information in a single integer.
Another purpose could be the following: generate hundreds of thousands of levels randomly. Some are bad (too hard, even impossible to win, or just too boring and easy, or even have weird bugs in them), then manually filter through them to find the ones that are actually decent-- store all of those seed values into an array somewhere, then whenever starting the same you could pick one of those stored seed values randomly, thereby generating a "random" environment but also only using the good ones. So you wouldn't need to perfect the random generating algorithm, just check that all of the, say, 100 combinations you have are allowed.
Of course at the same time it would be almost impossible to predict or design a seed number to do what you want. [I suppose the algorithm could be manipulated a bit so that, say, the first digit determined the difficulty, if you wanted that. It wouldn't, then, be truly random, but that could be useful.]
What do you think? Is this something that's established? I haven't thought about it before, but I find it very interesting.
[Note: in case anyone isn't familiar with the term seed as I'm using it, I'll explain quickly: when a computer generates a "random" number, it doesn't actually do anything random. It takes some value, such as the time of day, then uses that to "seed" the random number process. For example, it might take the seed value, square it, apply some kind of distortion algorithm (I've used MD5 in the past), and then base decisions on that. So nothing is really random if you set the seed number the same each time. If you manually set it to, for example, "1" then you'll always have the same "random" results. If you automatically set it to something that changes like PHP's microtime() which gives decimal values after the time of day in seconds, that'll be a reliable way to probably have a different page each time you load it. But I'm wondering here if you could use this to your advantage to control what happens with your actually non-random algorithm.]