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

joedfuseAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
You cannot use '%' with floating point values, modulus only works with integers by definition. To circumvent that, you could either use a library (e.g. boost provides that funcionality) or a slightly changed algorithm, e.g.

double randomDouble(const float& from, const float& to)
{
    double f = (double) rand() / RAND_MAX;
    return from + f * (to - from);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
joedfuseAuthor Commented:
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
0
jkrCommented:
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

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Julian HansenCommented:
As a matter of interest why did you change the randDouble code from that suggested here?
Copied from above post ?
double randomDouble(double from, double to)
{
    return from + ( ( ( double ) rand() / RAND_MAX ) * ( to - from ) );
}

Open in new window

0
sarabandeCommented:
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
0
joedfuseAuthor Commented:
Both solutions worked well. Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.