TIGSource Forums

Developer => Technical => Topic started by: timetocode on February 15, 2012, 12:57:17 AM



Title: procedurally generated content - Qs about prime numbers, seeds and value noise
Post by: timetocode on February 15, 2012, 12:57:17 AM
Hello! I'm new around here. I have recently become obsessed with noise generation and I had some questions. I've been overlaying perlin-esque noise maps with some fun effects (images available in my signature). I've got no issue using the noise algos at a basic level, but I want to understand more about what is actually happening.

I've been using algos that are predominately value noise, based around Hugo Elias' writings.

The PRNG is as follows:
Code:
  function IntNoise(32-bit integer: x)
    x = (x<<13) ^ x;
    return ( 1.0 - ( (x * (x * x * 15731 + 789221) + 1376312589) & 7fffffff) / 1073741824.0);   
  end IntNoise function

Why is this function a good candidate for a PRNG?
What do the prime numbers do?
I've tried replacing the prime numbers with non prime numbers and sometimes this resulted in the generation of a repeating pattern. Other times however, I've removed a prime number and the pattern does not repeat (or at least it doesn't obviously repeat).
Currently I treat the prime numbers as seeds. If I want a new seed then I have to find a similarly sized prime number to replace one of the above. Is there another way to seed this type of noise?


Title: Re: procedurally generated content - Qs about prime numbers, seeds and value noise
Post by: randomnine on February 15, 2012, 07:24:03 AM
It's a good candidate for a PRNG if its output feels sufficiently random. There are statistical tests for this (http://en.wikipedia.org/wiki/Pseudorandom_number_generator#BSI_evaluation_criteria) you can apply, if you like. That's all there is to it.

The magic numbers should have been chosen to make sure the generator cycles through a good fraction of possible 32-bit integers in a non-obvious way without repetition. Large primes are obviously good for avoiding repetition.

You "seed" most PRNGs by starting them with a different value. I assume you're calling something like

Code:
IntNoise(0)
IntNoise(1)
IntNoise(2)
IntNoise(3)

to get a bunch of different random numbers, right? Well, do this instead:

Code:
IntNoise(0+seed)
IntNoise(1+seed)
IntNoise(2+seed)
IntNoise(3+seed)

...and you'll get a different result for each seed. That said, depending on your algorithm you might have to pick quite different seeds to avoid having the same sequences pop up and cause identifiably similar patterns.


Title: Re: procedurally generated content - Qs about prime numbers, seeds and value noise
Post by: st33d on February 15, 2012, 07:30:36 AM
From wikipedia:

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself

So if you want a non repeating pattern, prime numbers are pretty useful.

Eg: If you wanted to set up a schedule list for tasks for a computer to perform, you could set them up to perform them at prime number intervals and they won't overlap.


Title: Re: procedurally generated content - Qs about prime numbers, seeds and value noise
Post by: timetocode on February 16, 2012, 05:49:56 AM
Thank you both for explaining.

Looks like I'll be seeding like this:
Code:
public static int Noise(int seed, int x, int y, int frequency, int octaves)
{
     x += seed;
     y += seed; //seed could be added to either or both dimensions to select a unique range
     //PNRG code omitted
}
That's for making 2d noise maps usually no bigger than 5000x5000 -- so if I pick seeds that are are 5000 apart the noise should be completely unique.

The full algo is here if anyone is interested: http://www.java-gaming.org/index.php?topic=23771.0 (http://www.java-gaming.org/index.php?topic=23771.0). DzzD was nice enough to release it as LGPL.