What you want to do is something like this:
double RealRand (double up_to)
{
return double(rand()) / RAND_MAX * up_to;
}
Effectively, what this does is first generate a random number between 0 and 1 (left-inclusive,) and then scale the number to [0..up_to
]. (Note: just in case you didn't know, rand()
function generates random integers between 0 and RAND_MAX
.)
However, there are (at least) two reasons you don't want to do this. First, rand()
is usually a bad random number generator. And second, on some platforms (e.g. Visual C++) RAND_MAX
is a very small number (32767 or something,) which leads to very poor resolution for the generated real values.
Using the facilities in <random>
header (newer C++) is highly recommended.