How does seed generation work in a video game?

352 views

How does a random string of characters, or in the case of some games (Rimworld) manage to generate a whole world with a single word?

In: 6

4 Answers

Anonymous 0 Comments

The world is not generated by the seed, the world is generated by a Pseudo-Random Number Generator (PRNG).

A PRNG can produce a stream of random-looking numbers, but they are not random – they are entirely determined by the starting state of the PRNG. Use the same starting state twice, you get the exact same stream of “random” numbers twice.

This starting state is called a “seed”.

Anonymous 0 Comments

All computers can do is math. Very complex and complicated math, but math nonetheless. If you take a system of mathematical operations and give it the same inputs, it will always produce the sane outputs. This is good; we want 1+1=2 to be consistently true.

But this is bad for games; every game will be the same, so you’ll know what will happen, where to go, what to do. So if you put a random seed in there (for example, the number of seconds that have passed since midnight January 1st, 1970), then you can introduce differences in the outputs of the mathematical equations. You might have to do some finagling with the math so that the continuously increasing “seed” doesn’t shift all the values one way, but there are some mathematical operations that can be bounded so that you aren’t going “off the edge”, so to speak.

Anonymous 0 Comments

When the world is being generated, the game has to make lots of decisions. What goes in this tile? Where does the player start? Is there an enemy here? Etc.

It usually makes these decisions at random. If there’s a 5% chance an enemy can be on any tile, then generate a random number from 1-100 and make an enemy if the number is 5 or less. (Actual open world generation is much more complicated because a random amalgamation of tiles and stuff doesn’t feel like a “world,” but the relationship to random numbers is essentially the same)

The thing with computers is that they are not really capable of randomness, so they simulate it. (Pseudo) random number generators can take a number and give out a number that appears random, but it’s really just the old number put through a bunch of mathematical functions. To generate another number, it just takes the last one and puts it through again, and so on forever.

The ‘seed’ in this process is the very first number the generator uses. If you repeat the process with the same seed, every “random” number generated will be exactly the same as it was before. Therefore every choice is the same, and the final world is the same. This can be a problem in other applications, but for games its just fine, because (as you’ve noted) it gives players a compact way (just the seed number) of sharing worlds they like or find notable.

Anonymous 0 Comments

You can design a box that takes a bunch of bits as input, and runs a calculation that thoroughly “mixes up the bits.”

The box’s output “looks random.” For example:

– If you feed the box a 1, you get 815965.
– If you feed the box a 2, you get 462141.
– If you feed the box a 3, you get 122526.
– If you feed the box a 4, you get 007855.
– If you feed the box a 5, you get 095046.
– If you feed the box a 6, you get 955227.

There’s no obvious rhyme or reason to the output, it’s a bunch of random 6-digit numbers.

The details of how the box works internally could be a whole nother post. What’s important is that those internal workings are a computer program that always does the same thing if you give it the same input. So:

– If you feed the box a 1, you get 815965.
– If you feed the box a 1 again, you get 815965.
– If you feed the box a 1 a third time, you get 815965.

Different inputs? Random. Same input? Same output.

You can use the output numbers to make various decisions about the world. For example the first two numbers were 815965 and 462141. Okay, we’ll make point (815, 462) on our 1000×1000 world grid to be the location of some ruins.

Let’s say there’s a 20% chance for plains, a 30% chance for forest, and a 50% chance for mountains. So if our next number is between 000000 and 199999 it will be a plains, if it’s between 200000 and 499999 it will be a forest, if it’s between 500000 and 999999 it will be mountains.

The next number is actually 122526 so our ruin is on a plains.

We don’t want one isolated plains tile to be jumbled into a bunch of mountain and forest tiles. We want our ruins to be in the middle of a plains patch. So we use more random numbers to decide the size and shape of the patch.

There could be thousands or millions of random numbers involved in generating the whole world.

So where does the seed come into the process?

Well we started our random sequence by inputting 1 to the random number generator function. On the game setup screen we add a box where the user can enter a seed, and then if the user enters say 5678, instead of feeding the box 1, 2, 3, … we instead feed the box 5678000001, 5678000002, 5678000003, …

Feeding the box a different sequence will give you different results. So instead of our ruins being located at (815, 462) in a 12-tile patch of plains, the box will give us different output. Meaning, we’ll decide to put the ruins at (337, 406) in a 17-tile patch of mountains with a different shape.

If the user gives the game the same seed, all the rolls are the same. When we run the world generation process again with the same seed we used before, we’ll put the ruins at the same place, pick the same terrain, pick the same size and shape for the terrain patch, and so on. Every choice will be the same, so an identical world will be created.

The random generation process only gives us the base world. We have to use a separate system to keep track of modifications the player’s made to the world, and apply them in a separate step.