double randDouble(double low, double high)
{
double temp;
/* swap low & high around if the user makes no sense */
if (low > high)
{
temp = low;
low = high;
high = temp;
}
/* calculate the random number & return it */
temp = (rand() / (static_cast<double>(RAND_MAX) + 1.0))
* (high - low) + low;
return temp;
}
function rand returns an integer in range from 0 to RAND_MAX. to make a float from it, you have to divide it by some float (if you divide an int by another int the result will be int as well).
for example:
float r = rand()/(float)RAND_MAX;
this generates a random float value in range from 0 to 1.
(float) as well as static_cast<double> converts RAND_MAX to a float.