3

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?

Bruno Bronosky
  • 66,273
  • 12
  • 162
  • 149
Tony
  • 81
  • 1
  • 3
  • You should really read a book on Objective-C. `%` is a common integer arithemtic operator which any decent book will cover in detail. – Paul R Jan 22 '12 at 17:04
  • It's plain C, which is probably a better place to be searching for this kind of reference, as Objective-C references tend to focus on what Objective-C does over and above C. – Abizern Jan 22 '12 at 17:06
  • What on earth is the question here? You say you know it's the modulus operator; if you know that, then you know "how it works". Did you try it out and saw something strange happening? If you see this pattern "in all examples", then what reason do you have to even suspect that this is in any way a special use? – jscs Jan 22 '12 at 17:34
  • I have been doing JavaScript for some time now, there you use rand() function to generate a random number from 0 to 1. Then, maybe Tony thought arc4random worked in the same way, and seeing the modulus operator acting on a decimal value is confusing (and maybe nonsense). Off course, that is not the case with arc4random which generates an integer. – MrAn3 May 04 '14 at 01:41
  • 3
    **We have to be a better community.** The original wording of this question was a disaster. Clearly this is not a native English speaker and with a reputation of `21` he is new here. We should welcome him by fixing his question. All it took was a little grammar and a few backticks and it looks like a every other SOQ. Remember, you too were once newb. I'm reopening this question. Thank you @ksol for the great answer. – Bruno Bronosky Jan 06 '16 at 08:00

1 Answers1

12

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.

ksol
  • 11,835
  • 5
  • 37
  • 64
  • 1
    0..77, including 77; is a better way to say things here – pnizzle Apr 11 '14 at 01:52
  • 1
    @pnizzle `[0, 77]` is how you write 0≤x≤77 & `(0, 77)` is how you write 0 – Albert Renshaw Sep 01 '14 at 01:43
  • Right brackets denote "in range including x". Parens denote "in range up until x" – Albert Renshaw Sep 01 '14 at 01:44
  • @AlbertRenshaw I was just trying to say that 0≤x≤77 means "upto and including 77"; better than "upto 77 and excluding 78" as christian had mentioned. His comment is correct, but is better said as "including 78". – pnizzle Sep 01 '14 at 02:01
  • 1
    @AlbertRenshaw thanks for the info on the brackets, I was not aware of that. – pnizzle Sep 01 '14 at 02:07
  • 1
    Just for good record keeping, here's a more reliable source and more detailed report on open and closed interval notation: http://en.wikipedia.org/wiki/Interval_(mathematics)#Excluding_the_endpoints – Albert Renshaw Sep 01 '14 at 02:52
  • Thank you @ksol for the detail on the range being less than the right operand of the modulus. This is what people will show up here looking for. This is the first explanation I've seen that clarifies that point. And it is an important one because you can't just run the code and see what the result is... it's random! – Bruno Bronosky Jan 06 '16 at 08:07