Please explain the logic used here.

the images are in the link here:

Please explain the logic used here.

the images are in the link here:

The standard template library function rand() returns random numbers between 0 and 32767.

rand() uses an internal mathematical formula to create a random number.

This formula uses a variable number called a “seed”. If you don’t provide this “seed”, rand() uses a default number for that.

Therefore, rand() will always give you the same random number if you don’t change the seed.

To change the seed, we use srand(), which stands for “seeded rand()”. You would pass a parameter to it that changes automatically every time.

and that it is why we use the time() function to give the number of seconds since 01-Jan-1970 until now. Every time you run your program, a second or more pass by, and the number of seconds produced by time() increases. Hence it changes with time.

As to why we pass zero to time(0), this means return the current time.

As for getting a random dice number, the formula used in the video is:

[rand() % (maxValue - minValue + 1)] + minValue.

To be honest with you, this formula is generic one to pick any random digit for a range between [minValue, maxValue], not only for rolling a dice, but could also be picking a random car number for a fleet that consists of 10 cars.

In this specific case of having a dice, the minValue and maxValue are known constants. Therefore:

(maxValue - minValue + 1) is always 6

Therefore, specifically for rolling the dice, the formula can be simplified to:

(rand() % maxValue) + minValue

So, when we divide a random number by 6, the remainder could be between 0 and 5. We then shift the result by 1 (i.e. + minValue) to get a range between 1 and 6.

I hope this makes sense.