Search code examples
iosobjective-cxcoderandomarc4random

arc4Random() - generate numbers "jumping after 10"


I'm not sure how to format arc4Random() to generate random numbers between 200 and 300. I would like numbers like 200, 210, 220 and so on.. (not 200, 201, 202, ...) (Xcode 5.1.1, iOS)

Any idea?

My piece of code:

self.currentObstacX += arc4random()%(200+10) + 300;

... but it looks like it does not work how I need.

Thank you in advance.


Solution

  • @PaulR answer will work but it is better to use arc4random_uniform like this...

    NSInteger number = 10 * arc4random_uniform(11) + 200;
    

    From the docs...

    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.