-2

I want to add two CGFloat numbers and display them using random numbers, but don't know how to do that.. My code is :

CGFloat latitude = 31.303702;
CGFloat longitude = 75.594905;

and the value I want to add to both cgfloat values is

CGFloat randomValue = 0.000001;

and display them using random numbers. Can anyone help me... The result will be like 31.303703 & 75.594906.

Note: The value of latitude and longitude is dynamic and its coming from an array.

Alladinian
  • 34,483
  • 6
  • 89
  • 91

1 Answers1

1

Have you tried just adding the the two and using NSLog to print on console :

NSLog(@"Latitude after Random Addition : %f", latitude+randomValue);
NSLog(@"Longitude after Random Addition : %f", longitude+randomValue);

Update 1:

Use following method to generate a random CGFloat between a given range:

- (CGFloat)randomFloatBetween:(CGFloat)smallNumber and:(CGFloat)bigNumber {
    CGFloat diff = bigNumber - smallNumber;
    return (((CGFloat) (arc4random() % ((unsigned)RAND_MAX + 1)) / RAND_MAX) * diff) + smallNumber;
}

You can call it as:

CGFloat randomValue1 = [self randomFloatBetween:1 and:10];
CGFloat randomValue2 = [self randomFloatBetween:3 and:5];

NSLog(@"Latitude after Random Addition : %f", latitude+randomValue1);
NSLog(@"Longitude after Random Addition : %f", longitude+randomValue2);
UditS
  • 1,936
  • 17
  • 37