jeudi 15 décembre 2016

How does std::rand() % number work and why?

This is probably the question about me understanding how exactly modules operator work and i have been exploring the stackoverflow for any available answer on this topic and unfortunately, i can't connect these in a reasonable way.

When i use:

int result = std::rand();

as a result i get the number between 0 and MAX_INT. Right?

But when i use:

int result = std::rand() % 5;

I get a number between 0 and 4, which makes no sense to me.

If i run std::rand() % 5 in a loop, on every iteration i will be getting a random number between 0 and MAX_INT and then divide it by 5 and putting the remainder into result variable but it's not how it works.

What am i missing here?




Aucun commentaire:

Enregistrer un commentaire