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

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


Open in new window

Who is Participating?
jkrConnect With a Mentor Commented:
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

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Julian HansenConnect With a Mentor Commented:
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

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.

joedfuseAuthor Commented:
Both solutions worked well. Thanks
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.