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:
- In each run, the same set of random numbers does not change!
- 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