Question

I am using this implementation of the Mersenne twister for a diamond-square terrain generator I am writing. The point of using a separate implementation rather than the built in rand() is that I want the same seed to generate the same map every time. Diamond-square requires a random error to be added on to every pixel, so I need to generate many, many random numbers from a single seed. Mersenne twister would be good for this, but as far as I can tell, this implementation generates only one random number. I have replaced the rand() in mt_init() with an integer argument. I can make no further head or tail of the code, however, so I must turn to you to ask: what steps should I take so that mt_random(n) returns the nth random number in the Mersenne twister series it belts out?

Was it helpful?

Solution

If you need more random numbers, just keep calling mt_random as many times as you want.

OTHER TIPS

I would reconsider using rand and srand. If you want a separate random function whose state isn't affected by normal calls to rand, you can use something like this.

unsigned int my_seed = 42;

int my_rand()
{
    srand(my_seed);
    return my_seed = rand();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top