Random number precision

I’m trying to generate random GPS locations withing a certain range; a square area defined using min and max positions.

For example:

float lat = ofRandom(51.954587, 51.992465);
float lon = ofRandom(5.891907, 5.964681);
ofLog() << lat << ", " << lon;

This results in a position like:

51.9607, 5.89286
51.966, 5.91742
51.9899, 5.91362
51.9913, 5.93712

As you can see the precision of the results is lower (maximum of 5 decimals) than the numbers entered into ofRandom() (6 decimals), meaning there is a larger chance of getting ending up with the same location, plus the resulting list of points is not as dense as I’d like it to be.

Does anyone know a way to generate numbers with a higher precision?

I just did a quick test and my impression is that it is actually the cout who is cropping the value contained in that float to a maximum of 5 decimals.

you can try:

ofLog() << ofToString(lat, 10) << ", " << ofToString(lon, 10);


std::cout << lat << ", " << lon << std::endl;

My float precision issues in c++ are a bit rusty, so I could be missing something. I would suggest looking at c++ forums if precision is really important to you.

It seems you’re right and ofLog() is indeed rounding the numbers while the actual floats contain numbers with more precision. I’m not quite sure how it decides to use 3/4/5/6 digits, but it doesn’t really matter.
Thanks for the feedback.