arc4random()
returns an unsigned integer, 1 + (arc4random() %120) - 60;
is interpreted as unsigned, then assigned to NSInteger random1
.
You can either cast the return value of (arc4random() %120)
to a signed integer:
NSInteger random1 = 1 + (NSInteger)(arc4random() %120) - 60;
or store the intermediate result of arc4random()
in an unsigned integer:
NSInteger r = (arc4random() %120);
NSInteger random1 = 1 + r - 60;
Also note that the arc4random man page recommends using arc4random_uniform()
over modulo constructs:
arc4random_uniform() is recommended over constructions like ``arc4random() % upper_bound'' as it avoids "modulo bias" when the upper bound is not a power of two.