I think this is common if the random generator algorithm leaves a certain pattern of bits as zero. (For example, if the low-order bits are zero, the number mod some low constant will always be zero.)
Maybe you should try something like:
const int desired_maximum = /* ... */;
int r = (((double)rand()) / RAND_MAX) * desired_maximum;
For example in this manpage link, it says:
In Numerical Recipes in C: The Art of Scientific Computing (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling; New York: Cambridge University Press, 1992 (2nd ed., p. 277)), the following comments are made:
"If you want to generate a random integer between 1 and 10, you should always do it by using high-order bits, as in
j = 1 + (int) (10.0 * (rand() / (RAND_MAX + 1.0)));
and never by anything resembling
j = 1 + (rand() % 10);
(which uses lower-order bits)."
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…