Public Group

# Need help understanding Modulus

This topic is 3835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Someone explain how it works. The only thing I know is that modulus arithmetic operator is represented as "%"
Quote:
 #include #include #include using namespace std; int main() { srand(time(0)); int randomNumber = rand(); int die = (randomNumber % 6) + 1; cout << "You rolled a " << die << endl; cin.ignore(cin.rdbuf()->in_avail() + 1); return 0; }

##### Share on other sites
It returns the remainder of dividing two integers. In that example it's bringing a random number into the range 0 to 5.

##### Share on other sites
It gives the remainder in a division of two (positive - I'm not sure that it's not implementation-defined behaviour for negative) integers:

3 % 2 = 1
4 % 2 = 0

It's handy for looping around at a certain value and for detecting whether a number is exactly divisible by something, as well as having utility in various areas (such as hashing and cryptography).

0 % 3 = 0
1 % 3 = 1
2 % 3 = 2
3 % 3 = 0
4 % 3 = 1
5 % 3 = 2
6 % 3 = 0
7 % 3 = 1
8 % 3 = 2
9 % 3 = 0
...

Got it?

##### Share on other sites
Quote:
 Original post by return0It returns the remainder of dividing two integers. In that example it's bringing a random number into the range 0 to 5.

The modulus operator is a binary operator used to give you the remainder of dividing two operands. It's called a binary operator because it takes two operands and returns the remainder. For example if you have 23%3,the remainder will be 2, because 23/3 is 7, and 3*7 = 21, and take that and subtract it from 23 and you get a remainder of 2.

##### Share on other sites
Oh ok I got it.
So if the random number is say 848:

848 / 6 = 141.33333
Ignore everyhthing beyond "."
141 * 6 = 846
848 - 846 = 2
2 + 1 = 3

So the random number is 3.

Thanks.

##### Share on other sites
Yep, I think you got it. Good job.

Here is a link telling you a little more about the modulus operator. I think you should just read the first paragraph. Below the first paragraph things tend to get cryptic. Happy coding and good luck.
http://en.wikipedia.org/wiki/Modulo_operation

Since you are learning about the modulus operator, I think you should also be aware of order of operations. I tried finding a link that discusses order of operation but I could not find one. I'm going to look in one of my old c++ book to see if I can find something that talks about order of operation.

[Edited by - boolean010 on July 24, 2008 6:38:01 PM]

##### Share on other sites
Quote:
Original post by boolean010
Quote:
 Original post by return0It returns the remainder of dividing two integers. In that example it's bringing a random number into the range 0 to 5.

The modulus operator is a binary operator used to give you the remainder of dividing two operands. It's called a binary operator because it takes two operands and returns the remainder. For example if you have 23%3,the remainder will be 2, because 23/3 is 7, and 3*7 = 21, and take that and subtract it from 23 and you get a remainder of 2.

Why quote me if you're just going to repeat what I said in a more convoluted way?

##### Share on other sites
Ok ;)
Thanks for support!

##### Share on other sites
Quote:
 Original post by TheUnbelieverIt gives the remainder in a division of two (positive - I'm not sure that it's not implementation-defined behaviour for negative) integers:3 % 2 = 14 % 2 = 0It's handy for looping around at a certain value and for detecting whether a number is exactly divisible by something, as well as having utility in various areas (such as hashing and cryptography).

It also comes in handy when you want to limit the number of values per line you want to output for example:
[source lang = "cpp"]	// output 5 primes per line	for(int i = 0; i < max; i++)	{		if(i % 5 == 0)			cout << endl;		cout << setw(10) << *(pprime + i);	}

You can change the 5 to a smaller or bigger number depending on how many numbers you want per line.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 15
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
634151
• Total Posts
3015819
×