0

I'm wondering how one could use a PRNG with a given seed to get same results even when multithreading the process.

Clarifications:
The world will be using a Coherent Noise such as Perlin or Simplex to generate height, this noise would be determined by a World Seed. Then I would want each chunk to generate some randomness such as how many trees are placed within the chunk, where the trees are placed, how many flowers should be placed and etc. This randomness should be the same every time the same World Seed is given. The chunks could be loaded in different order each run of the game depending on what direction the player came from, (If the player came from the north it would have a different order then if the player came from the south).

How do I use a Seeded RNG across threads in a way that chunks can be generated in any order and still get the same result as it did the last time.

Edit:
The solution is probably using the chunk position (which would be x, y Coordinates) and the world seed to generate a new PRNG for each of the chunks. However this would mean that each chunk would need to generate a new PRNG to get some amount of random numbers, is that really the best way to avoid the problem?

hampus toft
  • 55
  • 10
  • To paraphrase: since the "chunks" can be created in arbitrary order it's not really a question of the seed per se, rather that the sequence of random numbers is meaningless because the "chunk" a given RN applies to will change based on rendering criteria? – Dave Newton Jun 08 '21 at 18:18
  • 2
    Determine some kind of chunkId (coordinate based for example), and have `chunk seed = world seed | chunkId` for per-chunk RNG? – Kayaman Jun 08 '21 at 18:24
  • Related: https://stackoverflow.com/questions/67466830/rebase-pcg-seed – Peter O. Jun 08 '21 at 18:28
  • What's wrong with having a per-chunk RNG? Do you assume a PRNG is somehow resource heavy? – Kayaman Jun 08 '21 at 18:44
  • 2
    I would do the per-chunk RPG thing. It's the most obvious and easiest; also then you don't have to synchronize your RNG and make them threadsafe, nor do you have contention from using a shared object. Win win win. – markspace Jun 08 '21 at 18:57

1 Answers1

0

There should be no issue with multi-threading as long as you work on chunks in parallel (each thread a chunk for example) this is true for perlin noise as knowledge of other chunks is not needed. The only problem you will have is determining the size of the desired chunks, not too small and not too big ... Concerning Java look into thread pools: https://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html

Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85