From: matt m. <ma...@cs...> - 2003-04-16 21:41:17
|
albert- there is nothing to say here except you are right. painfully right. i'm going to check in your changes to CVS now and it will be a part of the next release. thanks for catching this error. -- matt Today, Albert Chu wrote forth saying... > Hello, > > I'm not sure about all architectures, but for most architectures, I > believe random numbers will not be properly generated with the > rand_int() definition in monitor.c. I noticed this issue in ganglia > 2.4.1 running on Linux, but I believe the problem still exists in > 2.5.3. As of 2.5.3, the rand_int() definition is: > > #define rand_int(__min,__max) __min + ( __max * rand()/(RAND_MAX+1.0)) > > Problem #1: > > - rand_int() is called from set_check_threshold() and set_mcast_threshold(). > > - These two functions pass in metric[key].check_min, > metric[key].check_max, metric[key].mcast_min, and > metric[key].mcast_max as values to the rand_int() definition. > > - check_min, check_max, mcast_min, and mcast_max are defined as > integers in metric_typedefs.h. > > - Thus the following problem comes up ... > > rand() = "0 <= integer <= RAND_MAX" > > __max * rand() = "INT_MIN <= integer <= INT_MAX" > - because of possible maximum integer overflow > > (__max * rand())/(RAND_MAX+1.0) = "-1 < float < 1" > - because numerator is always smaller than denominator > > (int) (__min + "-1 < float < 1") = __min or (__min - 1) after truncation > > By making __max a float, the integer overflow does not become an > issue, and a random number is properly generated. > > Problem #2: > > I also believe that rand_int() is incorrect because it only works when > __min == 1 (which is what is in the Linux manpage for rand()). > > I believe the code ((__max * 1.0) * rand()/(RAND_MAX+1.0)) will actually > produce a random number from 0 to (__max - 1). Thus, this works > perfectly for when __min is 1, but not when __min is larger than 1. I > believe you want > > (((__max-__min)*1.0) * rand() / (RAND_MAX + 1.0)) > > My test code & solution: > > I ran it on Linux. > > #include <stdio.h> > #include <stdlib.h> > > #define rand_int(__min,__max) __min + ( __max * rand()/(RAND_MAX+1.0)) > > #define my_rand_int(__min,__max) __min + ( ((__max-__min)*1.0) * > rand()/(RAND_MAX+1.0)) > > int main() { > int i,num; > srand(1); > > for (i = 0; i < 25; i++) { > num = rand_int(10,20); > printf("%d\n", num); > } > printf("\n"); > > for (i = 0; i < 25; i++) { > num = my_rand_int(10,20); > printf("%d\n", num); > } > printf("\n"); > > } > > Thanks > Al > > > > ------------------------------------------------------- > This sf.net email is sponsored by:ThinkGeek > Welcome to geek heaven. > http://thinkgeek.com/sf > _______________________________________________ > Ganglia-developers mailing list > Gan...@li... > https://lists.sourceforge.net/lists/listinfo/ganglia-developers > |