Generate random number between -150 and 150

Discussion in 'iOS Programming' started by Jeremy1026, Aug 12, 2008.

  1. Jeremy1026 macrumors 68020

    Jeremy1026

    Joined:
    Nov 3, 2007
    #1
    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];
     
  2. calculus Guest

    calculus

    Joined:
    Dec 12, 2005
    #2
    I don't know anything about programming, but could you generate a random number between 0 and 300 and then subtract 150?
     
  3. Encryptic macrumors member

    Joined:
    Jul 27, 2008
    #3
    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];
    
     

Share This Page