How to generate a random number within a range?

Asked

Viewed 222 times

4

Using the mercy of arc4random_uniform(), how should I set the logic to generate a Random within a certain range?

For example, given the range [5, 10]

Candidates for Andom:

5, 6, 7, 8, 9, 10

1 answer

5


As the function allows to determine the maximum, just need to make a minimum offset.

arc4random_uniform(max - min) + min

I put in the Github for future reference.

In his example:

arc4random_uniform(5) + 5

Browser other questions tagged

You are not signed in. Login or sign up in order to post.