In the program, I met a rather interesting moment of the rand() function and after it there are numbers.

 mas[i] = rand() % 105 - 5; 

I understand that this is the assignment of a random value to a cell with an index i in the mas array

Here interests that under this represents this record % 105 - 5 . I understand that this is a number from 0 to a random maximum and the remainder of dividing by 105 , well, that's what -5 . And why use it?

  • apparently need random numbers from -5 to 100. - Mike
  • @Mike Thank you corrected, and took note of it, that is, this way in C ++, it indicates how big the random number is if you specify% 204-5; will it be from -5 to 204? - Mr.Flatman

2 answers 2

The rand() function returns a pseudo-random value in the interval [0..RAND_MAX] . The % operator provides a residue for integer division. So writing rand() % 105 will give values ​​from the range [0..104] . Subtracting 5 this range will change to [-5..99] .

In this case, your option is sishny rather than plus. In modern C ++, it is recommended to use the functions declared in the <random> header file.

    Such a record assumes that the number from -5 to 99 inclusively will be written to mas [i].