Search code examples
objective-carc4random

arc4random and % operator


I have a question about the arc4random() function in Objective-C.

In the examples I have seen online, there is a % symbol right after the function call. I think of % as the modulus operator, so does this symbol have another meaning when used after arc4random()? How does it work?


Solution

  • No special meaning. Applying a modulus after arc4random() (or any other random function) is a common pattern to restrict the min/max of the random. arc4random() % 78 will return you a number between 0 and 77, for example.