I prefer to define the function so that a number in the range [0,1) is returened.. In other words, numbers as high as 0.9999... will be generated. With this definition, it is straightforward to define randbetween(x,y) in terms of this function.
Code:
// return a random double in [0.0, 1.0)
double randfrac(void) {
double res = (rand() % RAND_MAX) / (double)RAND_MAX;
return res;
}
// return random double in [x,y)
double randbetween(double x, double y) {
return randfrac() * (y-x) + x;
}
For seeding, you can use time(NULL) but it tends to give a poor seed if you run your program multiple times. I use the tv_usec value from gettimeofday
Code:
// call srand(3) using the time value in microseconds
void doseed(void) {
struct timeval tp;
gettimeofday(&tp, NULL);
unsigned seed = (unsigned)tp.tv_usec;
srand(seed);
}
Then just call doseed() once before calling any random number generating function