(1) Only seed rand at the very start of the program. Move your srand(time(NULL)) before the loop.
(2) % has higher precedence than +
- rand() % 6 can produce {0, 1, 2, 3, 4, 5}
- rand() % 6 + 3 can produce {3, 4, 5, 6, 7, 8}
my knowledge now think the number is between 6 and 3. |
If you want numbers in the range {3, 4, 5, 6} to be produced, this is 4 possible values,
so you would do rand() % 4 + the lowest number allowed.
So in your case,
rand() % 4 + 3
--> {3, 4, 5, 6}.
In general, if you want the inclusive range [a, b]:
rand() % (b - a + 1) + a
(Assuming a <= b)
e.g. [2, 5] --> rand() % (3 + 1) + 2 = {2, 3, 4, 5}