#include <iostream>
#include <cstdlib>
#include <ctime>
int main ()
{
srand (time (NULL));
double x, y;
std::cin >> x >> y;
double range = y - x;
double randFloat = rand () / RAND_MAX;
double randNum = (randFloat * range) + x;
std::cout << randNum << std::endl;
// just so you can see the result
int asdf;
std::cin >> asdf;
return 0;
}
Yes, but the bias is in the RNG, not the algorithm you posted.
For unbiased random numbers, you need to use a better RNG. Google around for one. Good luck!
rand() gives an int. RAND_MAX is an int. Therefore rand() / RAND_MAX will do integer division and the result is 0 (except if rand() returns RAND_MAX). What you have to do is to cast at least one side of the division to a floating point number. double randFloat = static_cast<double>(rand()) / RAND_MAX;