Consider using apr_time_usec
for desired random number. apr_time_usec seems to be "more random", because it returns usecond part (the whole time is sec + APR_USEC_PER_SEC * usec
) and is guaranteed to be inside [0 .. 1000000]
by definition, so you may don't worry about loosing precision with cast.
If using seconds, not useconds is crucial, then you may calculate your random number as
channel->id = (apr_int32_t) (apr_time_sec(apr_time_now()) % INT32_MAX)
(don't forget to include limits.h
) to fit it inside 32-bit diapason some smarter then just by truncation.