# How Does Modulus Divison Work

I don't really understand how modulus division works. I was calculating `27 % 16` and wound up with `11` and I don't understand why.

I can't seem to find an explanation in layman's terms online. Can someone elaborate on a very high level as to what's going on here?

20

The result of a modulo division is the remainder of an integer division of the given numbers.

That means:

``````27 / 16 = 1, remainder 11
=> 27 mod 16 = 11
``````

Other examples:

``````30 / 3 = 10, remainder 0
=> 30 mod 3 = 0

35 / 3 = 11, remainder 2
=> 35 mod 3 = 2
``````
Tuesday, June 1, 2021

57

If C++11 is an option then you should use the random header and uniform_int_distrubution. As James pointed out in the comments using rand and `%` has a lot of issues including a biased distribution:

``````#include <iostream>
#include <random>

int main()
{
std::random_device rd;

std::mt19937 e2(rd());

std::uniform_int_distribution<int> dist(6, 12);

for (int n = 0; n < 10; ++n) {
std::cout << dist(e2) << ", " ;
}
std::cout << std::endl ;
}
``````

if you have to use rand then this should do:

``````rand() % 7 + 6
``````

Update

A better method using `rand` would be as follows:

``````6 + rand() / (RAND_MAX / (12 - 6 + 1) + 1)
``````

I obtained this from the C FAQ and it is explained How can I get random integers in a certain range? question.

Update 2

Boost is also an option:

``````#include <iostream>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>

int main()
{
boost::random::mt19937 gen;
boost::random::uniform_int_distribution<> dist(6, 12);

for (int n = 0; n < 10; ++n) {
std::cout << dist(gen) << ", ";
}
std::cout << std::endl ;
}
``````
Wednesday, June 9, 2021

47

remainder of 3/7 is 3..since it went 0 times with 3 remainder so 3%7 = 3

Monday, August 2, 2021

86

The basic idea here is that you can take the modulus before, during, or after multiplication and get the same value after taking the modulus of the final result.

As @Peter points out,

``````(a * b) % m == ((a % m) * (b % m)) % m
``````

For the factorial,

``````n! = 1 * 2 * 3 * ... * (n-1) * n
``````

so we have

``````n! % m = (((((((1 * 2) % m) * 3) % m) * ... * n-1) % m) * n) % m
``````

taking the modulus after each iteration.

The advantage to doing it this way is that your number won't blow up and overflow your long long type like it would do pretty quickly if you didn't take intermediate modulus values.

Thursday, September 2, 2021

43

I don't want to bother you with some complex mathematical concepts, so i'll try to keep it simple. When we say that a = b (mod c), we simply say that a-b is a multiple of c. This means that when we want to know what is the value of a mod c, saying that it is a or a-c or a+c or a+1000*c is true. Thus, your 2 formulas are valid.

But what you want is to know the answer that a computer would give to you, right ? Well, it depends of the language you are using. With Java for example, a mod b has the sign of a and has is absolute value strictly inferior to b. This means that with a = 7, b = 3 and N = 5, (a-b)%N = 4, but your two expressions will return -1.

What I would suggest you to do if you want to do arithmetics with modulos is to create your own mod function, so it always give you a positive integer for example. This way, your 2 expressions will always be equal to the original one.

An example here in pseudocode :

``````function mod (int a, int N)
return (a%N+N)%N
``````
Tuesday, October 19, 2021