How to generate a random number within a range?

4

Using the arc4random_uniform() method, how should I assemble the logic to generate a random within a given range?

For example, given the range [5, 10]

Random Candidates:

  

5, 6, 7, 8, 9, 10

    
asked by anonymous 20.03.2015 / 21:05

1 answer

5

Since the function allows you to determine the maximum, you only need to do a minimum offset.

arc4random_uniform(max - min) + min

In your example:

arc4random_uniform(5) + 5
    
20.03.2015 / 21:11