i think you mean to use 'rand() % D + 1' and to assign that result to a different variable than D, since you need to retain user input.