This is a better way
(int)((double)rand() / ((double)RAND_MAX + 1) * N)
Get values in the range 0 - (N-1)
If you want 1-6 then N=6 and add 1 to the result.
If you don't want to use floating point arrithmatic you can use
rand() / (RAND_MAX / N + 1)
However if N is very close to RAND_MAX both these methods break down and you require something else.
It's all explained
here.