The line responsible for choosing the random word is
chosenWordNumber = aleatoryNumber(wordsNumber);
passing the count of found words as argument.
int aleatoryNumber(int maxNumber)
return (rand() % maxNumber);
function returns a random number between
library function returns a number between
and its result is clamped in the
range by the reminder operator.
Other solutions already explained broadly the full code behaviour.