Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Jeremy1026

macrumors 68020
Original poster
Nov 3, 2007
2,216
1,030
I am trying to generate a random number between -150 and 150, currently I know how to generate a random number that is either between 1 and 150 or -150 and 0. But I am having problems doing both.

Code:
xmove = [[NSNumber numberWithInt:1 + arc4random() % 151] integerValue];
 
I don't know anything about programming, but could you generate a random number between 0 and 300 and then subtract 150?
 
He posted this on another board, and I responded there as well.

But here's a code snippet of how to do it:

Code:
NSNumber *numResult = [NSNumber numberWithInt: (arc4random() % 300) - 150];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.