Question

I came across a bug in in a app which i was reviewing the bug was

unsigned int i = [[[NSString stringWithFormat:@"%u",(unsigned int) arc4random()]substringToIndex:2] integerValue]*[[NSDate date] timeIntervalSince1970];

this line of code which uses epoch time and arc4random() gives random no on simulator but on device returns a constant value 4294967295 every time.

though the bug itself was easy to solve but this mystery about the device and simulator difference is unsolvable for me i think it has to do with i386 vs armv.

Any explanation will be helpful.

Was it helpful?

Solution

NSInteger int1 = [[[NSString stringWithFormat:@"%u",(unsigned int) arc4random()]substringToIndex:2] integerValue] ;
NSTimeInterval t = [[NSDate date] timeIntervalSince1970] ;
double dd = t * int1 ;
NSLog(@"%f", dd) ;

unsigned int is not enough to place the value which is the result of integer * double.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top