This is simple, create a new Random()
object for each chunk, using that chunk's position as the seed.
If you think of Random
as a really (really) long series of numbers, the seed is just where to start iterating through. So, if you always set the seed before generating the chunk, you'll always get the same integers, in the same order.
EDIT
But how do you ensure that each chunk gets a unique seed?
It's not too critical to get a unique seed for each chunk, unless you want to try to guarantee that each chunk is unique from the last. I say try because even if each seed is unique it doesn't necessarily guarantee the random numbers you get will be different. If your world has a maximum size, you can get a unique long
from each position in the world with a method like this:
public long positionToLong(int x, int y, int z, int maxY, int maxZ) {
return (x * (MaxZ * maxY)) + (y * maxZ) + z;
}
If it doesn't have a maximum size, you can use some algorithm like:
public long positionToLong(int x, int y, int z) {
return x + y + z + (x*x) + (y*y) + (z*z);
}
Then you can do additional things to strive for unique seeds, like add the distance from the center of the world.
I should mention how I do this in my game. Since I'm generating terrain shape along with terrain type, I wanted to ensure that I'd have continuity between chunks. I did that by using a single world seed. Then the Perlin noise function takes the absolute world position as an input. This makes the transition between chunks seemless, which will not be the case if you're randomly generating each chunk distinct from the others.