-2

Can someone clarify to me please why past2 is NOT negative when this code is run? Even though past is.

Thanks.

NSTimeInterval p1 = (arc4random()%600000);
NSTimeInterval past = -p1;
NSTimeInterval past2 = -(arc4random()%600000);
WalterF
  • 1,345
  • 2
  • 11
  • 26

1 Answers1

2

arc4random() returns an unsigned int (u_int32_t), so trying to make it negative is coercing the result to unsigned as well, which is why you're getting a very large positive number instead of a negative number.

If you want to get a negative random result in one call, try:

NSTimeInterval past2 = - (int) (arc4random()%600000);

joe

Flyingdiver
  • 2,142
  • 13
  • 18