-1

I have some list of gps coordinates of latitude and longitude. How can I add random int value at the end of the float variable of latitude. I have tried this...

float log = 77.567635f;
Random rand = new Random();
int r = rand.Next(1, 20);

Let suppose value of r is 12, I would like to add to log '77.567635' + r.

My expecting result will be '77.567647'

Surya
  • 168
  • 4
  • 19

1 Answers1

0

What I would do is specify the precision of the floating point number first. If for example the precision is 0.000001 (like in your example) you can multiply the randomly generated number with this precision and add it to your coordinate.

float log = 77.567635f;
Random rand = new Random();
float r = rand.Next(1, 20) * 0.000001f;
log = log+r;
wserr
  • 436
  • 1
  • 3
  • 12
  • 2
    `float` does not have enough precision, you'll need to recommend `double`. – Hans Passant Nov 06 '19 at 18:59
  • @wserr thanks for your solution i got my expecting result `rand.Next(1, 20) * 0.000001` and @HansPassant, thanks for your suggestion to change the variable to double, it perfectly worked. – Surya Nov 08 '19 at 08:10