# "Rock, Paper, Scissors.." game in C++ project

I am trying to get logic of the expression : int computer = rand() % 3 + 1; in the project. Although I 've read the hint, I didnt quite understand the sense… Could somebody explain me that? Thank you.

I think random number generation is a pretty interesting topic, but it is also a bit complicated. It turns out that depending on the use-case, it’s not so easy to simulate true randomness.

So very loosely: we try to simulate randomness deterministically enough so that it “feels” random. If we flip a coin a hundred times, it will probably not alternate heads and tails in an even fashion, but probably be distributed with streaks involved. That’s some of the type of randomness that intended to be simulated (so not only the percentage, but the distribution as well).

The `srand` bit refers to a random seed generator. All the number generations are really based on this. If you have the same seed, you will always get the same numbers (so not random). You can look that up for more info.

The `rand()` method basically takes the numbers from this set of seed generated numbers.

So if i change the code to just

``````int computer = rand();
std::cout << computer;
``````

we get some large pseudo-random numbers

``````128127648
601050298
1354134796
``````

The `%3` bit basically guarantees that you will only get numbers in the range from [0,2] (0 to 2 inclusively). Because if you do any integer `%3` the answer is 0, 1, or 2. The plus 1 is so that the list is 1, 2, 3 instead of 0, 1, 2.

Now let’s play with the random seed.

``````#include <stdlib.h>
#include <iostream>

int main()
{
srand (42); // ;)
int computer = rand();
std::cout << computer;
}
``````

Given what was said above, what do you expect is going to be the type of the result the comes out when you run this program a couple of times? Will adding `% 3 + 1` change it’s essential nature?

1 Like

Hello. Thanks for your reply. I was interested if it is always %3 of any integer is 0, 1 or 2? Or just in this case. What is “essential nature” in the sense of this expression “%3 +1”?

So `% n` basically says, give me a range of `n` numbers. `+ x` says, starting from what number. `% 3 + 1` reads: give me 3 numbers starting from 1. (in this context)

But importantly, nothing from that is random (or pseudorandom). The real “randomness” happens at the seed level (which is why you should try seeing what happens with the different seed).

`% 3 + 1` in another context will not give you a random number. It will give you the remainder of division by 3 and add 1 consistently every time.

1 Like

So if %5 garantees that you will only get numbers in the range from [0,4]?

Yes, precisely!

Mathematically it works like this:

Pick a number, divide it by 5, return the remainder.

10003 / 5 = 2000.6
2000 * 5 = 10000
10003 - 10000 = 3
so 10003 % 5 = 3.

If you think about it, is it possible to divide a number and have a remainder of 5? No, because 5 divides 5. So it can only have remainders of 0, 1, 2, 3, and 4.

2 Likes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.