Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
1.42/5 (5 votes)
See more:
How to generate a random double number between 0 - 1.5?
Posted

In C++11 you can write this:
C++
#include <random>
#include <ctime>

double generate(double min, double max)
{
    using namespace std;

    static default_random_engine generator(unsigned(time(nullptr)));
    uniform_real_distribution<double> distribution(min, max);

    return distribution(generator);
}


...

generate(0, 1.5);
 
Share this answer
 
v2
Comments
Espen Harlinn 5-Nov-13 16:25pm    
5'ed! Good of you to point out that we now have a good api for random number generation as part of the standard library ;)
See rand_s:
http://msdn.microsoft.com/en-us/library/sxtz2fa8(v=vs.110).aspx[^]
Note the example which has generation of double random numbers in a range.
 
Share this answer
 
Comments
nv3 1-Nov-13 5:19am    
Thanks for the link. rand_s delivers a much better resolution that rand. 5.
Well, you can do it very simply.
C++
unsigned int nRand = 0;
rand_s(&nRand);

double dbValue = (double)nRand / UINT_MAX * 1.5;

Very simple, isn`t it?
 
Share this answer
 
Comments
WuRunZhe 30-Oct-13 23:43pm    
Could you accept my solution with blue button labeled "Accept Solution"?
Hi,
you can use this code:
XML
template <typename T>
T RandomGenerator<T>::generate(T lowerLimit, T upperLimit)
{
    static bool initialized = initialize();
    return double(rand()) * (upperLimit - lowerLimit) / RAND_MAX + lowerLimit;
}

template <typename T>
bool RandomGenerator<T>::initialize()
{
    srand(time(NULL));
}

double dVal = RandomGenerator<double>::generate(0, 1.5); //Generate doubles between (and including) 0 and 1.5.

T
 
Share this answer
 
Comments
nv3 1-Nov-13 5:16am    
Nice generalization. But why all the template stuff when you use double in the essential line?
see
http://stackoverflow.com/questions/2704521/generate-random-double-numbers-in-c/2704552#2704552[^]
Answer this address:
C++
double fRand(double fMin, double fMax)
{
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
 
Share this answer
 
v2
C#
double AOD;
  srand((unsigned)time(NULL));
  for (int i =0; i<10;++i)
  {
    AOD=((double) rand() / (RAND_MAX+1)) ;
    cout<<endl<<AOD;
  }
 
Share this answer
 
SQL
double fRand(double fMin, double fMax)
    {
        double f = (double)rand() / RAND_MAX;
        return fMin + f * (fMax - fMin);
    }

Reamamber to call srand() with a proper seed each time your program starts.
 
Share this answer
 
v2
C++
float RandomFloat(float 0, float 1.5) {
    float random = ((float) rand()) / (float) RAND_MAX;
    float diff = b - a;
    float r = random * diff;
    return a + r;
}
 
Share this answer
 
v2
Comments
mohamadzadeh62 9-Feb-14 15:44pm    
int main()
{
char A[10], B[10], s[20];
int i=0,j=0,l=0;
cout<<"Enter string1: ' '";
cin>>A;
cout<<"Enter string2: ' '";
cin>>B;
while (A[i]!='\0'|| B[l]!='\0')
{
s[j]=A[i];
s[j+1]=B[l];
cout<<s[j]<<s[j+1];
i++;
l++;
j+=2;
}



system ("pause");
return 0;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900