generate random double numbers in c++

Radi picture Radi · Apr 24, 2010 · Viewed 197.4k times · Source

How to generate random numbers between two doubles in c++ , these numbers should look like xxxxx,yyyyy .

Answer

rep_movsd picture rep_movsd · Apr 24, 2010

Here's how

double fRand(double fMin, double fMax)
{
    double f = (double)rand() / RAND_MAX;
    return fMin + f * (fMax - fMin);
}

Remember to call srand() with a proper seed each time your program starts.

[Edit] This answer is obsolete since C++ got it's native non-C based random library (see Alessandro Jacopsons answer) But, this still applies to C