I am trying to generate random numbers using drand48. I know, there are other random generators out there, but I need to use drand48. The code snippet is as follows:
For some reason, drand48() returns a negative number way above 1 (the range of the random double number needs to be between 0 and 1). What am I doing wrong?Code:double seedrandUI() { printf("\nin seedaranny\n"); struct timeval time; // seed random number generator gettimeofday(&time,NULL); srand48((unsigned int) time.tv_usec); return randInt48InRange(0, 1); // make int in [0,50000] range } double randInt48InRange ( int min, int max ) { printf("\nIN 48INITI\n"); printf("\nMin: %d",min); printf("\nMax: %d",max); double r;/* random value in range [0,1) */ long int M; double y; M = ((long int)(max-min+1)); printf("\nM is: %ld",M); r = ( drand48() / (double)(RAND_MAX / M + 1) ); printf("\nr is: %ld",r); y = r+min; printf("\ny as a long is %ld",y); printf("\nDRAND48 genereated: %ld \n",y); return y; }



LinkBack URL
About LinkBacks


