Hi guys

I'm trying to generate a radnom float number between 1.2 and 2.3 but when compile I get this error:

cal.cpp: In function `int main()':
cal.cpp:13: error: invalid operands of types `int' and `double' to binary `operator%'

the code:

int main()
{
  srand(time(NULL));

   float num;

   num = rand() % ( (2.3 - 1.2) + 1 ) + 1.2;

  cout << " Random number = " << num << endl;

 return 0;
}

Thanks for your help

Use something like

rand()*(1.1) + 1.2

Look up the fmod() function to replace the % operator.

Thank you all guys
and sorry for being late :)

actully I found this function and I like


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;
}

Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.