0

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

3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Jacky1
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.