1. ## drand48

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:
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;
}```
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?

2. You're making it way to complicated. try:
Code:
```#include <stdlib.h>
#include <stdio.h>

int main()
{
int i=0, j=0;

char tmp[12]={0x0};
for(i=0;i<50;i++)
{
double v=drand48() * 50001.;
sprintf(tmp,"%.0f",v);
j=atoi(tmp);
printf("%d\n",j);
}
return 0;
}```
output:
19824
42025
17667
22330
15935
44322
779
29205
7969
19186
34551
2943
44994
8177
7954
26654
30208
29136
13499
19524
14670
37120
14927
3777
20250
42870
47099
33142
42325
138
23119
26630
39395
13281
49139
15340
30043
30436
10622
44296
15233
7593
16883
19374
32181
37678
30181
26582
22968
32625

3. In my code snippet, I tried to seed the time so I can get random numbers everytime I run the program. I need to generate a random number from 0 to 1, but everytime I try to, I get a negative/positive number that is much much bigger than the range 0 to 1. Is there a way to get a random number from 0 to 1?

4. > printf("\nDRAND48 genereated: %ld \n",y);
Try compiling your code with -W -Wall command line options as well.
You don't use %ld to print a double.