They are using modular arithmetic to achieve a desired range. The equation gives you a range from zero to "M". In particular, you can see this is done in the "mod M" at the end of the equation. Basically, the left portion of the equation (i.e. the portion in parenthesis) is calculated and then the modulo operation is performed to the result
A brief description in case you are unfamiliar with modular arithmetic. The modulo operation solves for the remainder between the division of two numbers. For example, 5 mod 2=1, 1 mod 2=1, 6 mod 2=2, and 2 mod 2=2. The resulting answer will be between 0 and whatever mod value is used (excluding the value itself since x mod x=0). For more examples, I refer you to the google search engine; it is capable of performing this kind of math.
In this particular application, the choice of M limits the upper range of the output because there cannot be any remainders equal to or greater than M. I suggest you choose M to be 1 greater than the largest value you want.