Random Number Between 2 and 5

Discussion in 'Mac Programming' started by Darkroom, Aug 11, 2009.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    this isn't working. please help.

    Code:
    NSLog(@"%i", (arc4random() % (unsigned)5) + 2);
    
     
  2. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #2
    I think that will give you random numbers from 2 to 6. You should change the 5 to a 4. The % 4 will give you numbers from 0-3 then the +2 will change that to 2-5. If I'm reading it correctly.
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    FWIW...if you look at ScreenSaverView.h (spotlight it), Apple has some inline functions for generating random numbers (for copying into your source code). Kind of handy if you just need something quick.
     

Share This Page