The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value.
If no seed value is provided, the rand() function is automatically seeded with a value of 1.
To ensure a good distribution for a subrange of values, use code like the below:
i = RAND_MAX / my_range
i *= my_range
while ((j = rand()) >= i) continue;
return j % i;
(code example based on code from Karl Lehenbauer's fortune cookie program, which credits Ken Arnold, Unix Review, October 1987).