RAND_MAX is not guaranteed to be a divisor of ULONG_MAX, so take the
necessary precautions to get unbiased random numbers. Although the
bias might be negligible, it's not advisable to rely on it.
*/
static int RandomGetWrap(void)
{
- long int r = RandomGet();
- int r_int = r % (long int)RAND_MAX;
- return abs(r_int);
+ unsigned long r;
+
+ do {
+ r = RandomGet();
+ } while(r >= ULONG_MAX - (ULONG_MAX % RAND_MAX));
+
+ return r % RAND_MAX;
}
/*
*/
static int RandomGetWrap(void)
{
- long int r = RandomGet();
- int r_int = r % (long int)RAND_MAX;
- return abs(r_int);
+ unsigned long r;
+
+ do {
+ r = RandomGet();
+ } while(r >= ULONG_MAX - (ULONG_MAX % RAND_MAX));
+
+ return r % RAND_MAX;
}
/** \brief To initialize the stream global configuration data