dimanche 2 juillet 2017

Issue when Implementing a Random Number Generator

I want to implement an efficient random number generator in my program that generates a unique set of random numbers. To do so, I found linear congruential algorithm suits my requirement. The following is my customized code (It is also found here):

int rand();
int rseed = 0;
#define RAND_MAX_32 ((1U << 31) - 1)

inline int rand()
{
    return (rseed = (16807*rseed + 100) & RAND_MAX_32) >> 16;
}

However, I found two issues with this code:

  1. In each run, the same set of random numbers does not change!
  2. I cannot use my own modulus here. For instance, when my modulus is 100,000, changing RAND_MAX_32 to 100,000 does not work.

How can I fix these two issues? Thank you.




Aucun commentaire:

Enregistrer un commentaire