简体   繁体   中英

Need help converting C Mersenne twister implementation to be useful to me

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?

如果您需要更多随机数,则只需不断调用mt_random

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM