Link to home
Start Free TrialLog in
Avatar of joedfuse
joedfuseFlag for United States of America

asked on

C++ having problem with random generated double

Hello Experts,

I wrote a program that needs to generate random doubles/floats then apply them to other calculated functions. Everything works except the randdom double functions

Here is the code, note that all this code works fine if I used int's instead of doubles or floats. I get the error, Invalid Binary expression using Float in the randomDouble function line.

Thanks in advance

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>

using namespace std;

double randomDouble(const float& from, const float& to);

int main ()
{
	float lp0x, lp1x, lp0y, lp1y;
	float cmpx,cmpy;
	float radius;
    
    
	srand(time(0));
    
	lp0x= randomDouble(-99.9,99.9);
	lp1x= randomDouble(-99.9,99.9);
	lp0y= randomDouble(-99.9,99.9);
	lp1y= randomDouble(-99.9,99.9);
    
	cmpx= randomDouble(-99.9,99.9);
	cmpy= randomDouble(-99.9,99.9);
    
	radius= randomDouble(0.1,99.9);
    
   
    
	return EXIT_SUCCESS;
    
}

double randomDouble(const float& from, const float& to)
{
    return (rand() % (to - from + 1) + from);
    
}


}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joedfuse

ASKER

Ok I tested that and its close to what im looking for however...

The results are coming in with 4 decimal places. How would I restrict it to 1 eg 99.9

Thanks
Limiting the number of decimals is kinda pointless with floating point values - pardon the pun, but that's mainly a matter of 'display accuracy'. But 'floor()' would help you with that, e.g.

double round_to_decimals(double d, unsigned int dec) {

  double factor = pow(10.0,(double)dec);

  return floor(d * factor) / factor;
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you may use the below code if you want the full range as possible result

double randomDouble(const float& from, const float& to)
{
    // first, round both (10*from) and (10*to)  to integer
    // for example 12.345 -> 123
    int f = (int)(from*10. + 0.5);
    int t = (int)(to*10. + 0.5);

    int d10 = f - t ;  // d10 is an integer which can be used for modulo operation
    if (d10 <= 1)
          return f;
    int r = rand()%(d10+1);  // note we have an additional slot at the upper boundary

    // note we have to use f and not from, or the return value is not rounded to 1 decimal
    double rr = f + (r/10.) ;
    return rr;
}

Open in new window



note, the function can only work correctly  if the boundary values 'from' and 'to' were already rounded to one place after the decimal point. otherwise the function could return values which are either out of boundaries or never would hit the boundary values exactly.

Sara
Both solutions worked well. Thanks