dimanche 3 juillet 2016

Reseeding std::rand / c++11

I am trying to implement a simple noise function that takes two integers and return a random float based on the seed combined with the two parameters.

using std::mt19937 works great, but for some reason when I try to use srand with rand(), I get repeated numbers..

Note: Using c++11 seed member function in a loop is really, really slow.

here are two terrains using both methods (with the same reseeding numbers):

c++11 random:

std::random_device rd;
std::mt19937 g{ rd() };
std::uniform_real_distribution<float> gen{ -1.0, 1.0 };

float getNoise(int x, int z) {
     g.seed(x * 523234 + z * 128354 + seed);
     return gen(g);
}

c++11 random

c random:

float getNoise(int x, int z) {
        std::srand(x * 523234 + z * 128354 + seed);
        return static_cast<float>(std::rand()) / RAND_MAX * 2.0f - 1.0f;
}

enter image description here

To the questions:

  1. Is there a faster way to reseed the c++11 pseudo-random number ?
  2. Why doesn't the srand work as expected?

Please don't say I shouldn't reseeding, I want to reseed on purpose.

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire