Help with Random Generation

We are trying to archive random infinite chunk generation.

We are currently facing a problem, due to memory optimisation reasons we have to handle generation by each client individually. The problem that we are experiencing, is that we don’t know how to ensure that each client has the same chunks generated.

We already tried:

  • random.new() and random:NextInteger()
  • math.random() and math.randomseed()

Please let me know on how you would make each client generate the same chunks at the same coordinates, thank you.

1 Like

I am not sure how your system worked, but you should be able to make a system by setting the random seed with “math.randomseed”, using a number unique to each client. (UserId)

After setting the randomseed, generate it by using “math.random”.

You should be using math.noise to generate random chunks. It outputs the same values when given the same inputs.

We tried that, however, we want every client to be on the same map.

Setting math.randomseed() still somehow returned different numbers for every client.

We are currently using math.noise() to set the borders for different bioms. Can you inform me on how to use math.noise() to pick chunks from a folder?

math.noise gives an output on a rough interval of [-1, 1]. In order to use the value to directly pull a chunk out of a folder, I would shift the interval to [0, 1] by adding 1 to the output and dividing that value by 2. Optionally, you can use math.clamp to force the new value between 0 and 1. You could then multiply this value by the number of children in your folder and using math.ceil, so that this value cannot be 0.

After all this, you can use the value to index a chunk from within the folder and do what you will with it.

random.new(seed) should return the same values each time when you use things like :NextInteger() after it with the same values

Random.new(10):NextInteger(1,10) always returns 7

However, if players walk into different directions first, their numbers end up different.

It depends on how you’re doing it, to which I really have no clue, so I don’t know why direction matters.

If I were generating my chunks, i’d base everything on the coordinates.

As someone else stated however, a math.noise approach would likely be best. If you’re still looking for an answer, it may best to specify how you’re generating your terrain.

I simply use variatons of math.noise(x coordinate, z cooridinate) when im generating terrain, with some other maths ofcourse to generate decent terrain. I’ve never tried infinite terrain or a chunk based approach though.

We are not trying to get height variations.

We are simply having an infinite building with many 50x50 studs chunks to pick from.

Our end goal is a chunk system that is generated client-sided but is the same for every client. You can walk infinity in any direction

Ah sorry, spent some painful days working through a terrain generation algorithm so just assumed lol

If you base your Random.new(seed) seed on the center coordinate of the chunk then, for every player, the random output will be the same for each chunk. I’m assuming your using random to generate a number from which you’ll pick from a chunk.

chunks = chunkfolder:GetChildren()

--after checking if and where the chunk needs to be loaded then
random = Random.new(chunklocation.x.."|"..chunklocation.y)
chunkToLoad = chunks[random:NextInteger(1,#chunks)]

Should in theory pick the same chunk for each location every time, ofcouse im greatly assuming how your doing things.

2 Likes

Wait… does Random.new() take strings as well?

The solution I found based on your idea:

I added the two numbers together with the second one being always positive:

Random.new(Chunklocation.x..math.abs(ChunkLocation.y))

Thank you so much!

1 Like