Question

I have almost zero experience with random generators. I have used the standard rand functions in C and I have used perlin noise.

But now, I need a cube of resolution NxNxN (with N maybe 1e6 or bigger) filled with white noise (no perlin smoothness or whatever) in which I can just "pick" one value and if I repick that same value, it should be the same result every time. It should be seeded, so that if I start the application again, the results need to be the same. It should not be secure or whatever, just random enough so that a human being should not be able to predict values by just thinking. If he really analyses it with a computer, it's ok that it's predictable.

It's like I would use the standard C rand function, setting the seed and doing a nested for loop for the 3 dimensions and generating a random number every time until I reach the coordinate. That is of course terribly slow. I need something that's fast!

I have searched the internet and found a lot and nothing at the same time. Maybe I'm looking for the wrong keywords, but I haven't found anything I can use.

Can anyone help me get started? Code? A link? I don't really care if I understand the algorithms, but it should be easy to implement and use and should especially be fast.

Was it helpful?

Solution

Since nobody seems to be able to answer the question, I have been researching on this and I have solved it with the following sollution:

int pseudoRandom(int &seed)
{
    const int a = 16807; // 7^5
    const int m = 2147483647; // 2^31 - 1
    seed = int(unsigned(seed * a) % m);
    return seed;
}

I take the above, well known, random function. It's a good function as it can be easily be converted to a shader language, and can easily be expanded to 64-bit ints or something else.

Now, I need to be able to create randoms between 0.0 and ?.? on 3 dimensions and they should always have the same result with the same base-seed, so I took the integer part of every component x, y and z, and a base-seed.

int seed = baseseed;
pseudoRandom(seed);
pseudoRandom(seed * (int(x) + seed) * (int(y) + seed) * (int(z) + seed);

=> seed is now on the value where I can start making a detailed random grid. So if I need a detail of 32x32x32 random values, I just generate it here into an array. If I would have needed a bigger array, I could have done the same step again to generate another layer, like doing:

pseudoRandom(seed * (int(x * 100) + seed) * (int(y * 100) + seed) * (int(z * 100) + seed);

This way, you're actually quite free on how you randomize, and you have full control of the detail.

This solved my problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top