Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
If I write...

guess = arc4random() %3;

Will it select 0,1,2,3 or will it just pick 0,1,2?

I did test this but since the number is random I thought I would check.

Thanks
 

Hansr

macrumors 6502a
Apr 1, 2007
897
1
You'll get 0,1,2 not 3 since it's essentially from 0 to x-1 where x is the modulus number.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
You already got your answer, but don't seem to know what modulus is doing. It is the remainder of integer division of the left operand by the right operand. Knowing this, it's obvious that you'll never get the right hand operand as a result.

-Lee
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
Lee.... I did know what modulus was. I covered that in the book a few weeks ago and use it to find Prime numbers. That begin said, I did miss the % in this line of code and it's meaning.

Now that being said, I am confused why you need the Modulus to find the remainder when you are trying to get a random number? When I learned Python it was simple "random.randrange(1,10)"

So why do you need to use the Modulus? and how then would you select a random number from say 5 to 10?

Thanks again for pointing that out Lee so I can understand it.

-Lars
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.