How to set timeval to a random time

243 Views Asked by At

I am using libevent to do some tasks after some timeouts, timeouts are randomly selected, and each time they change. The problem is I cannot set timeval variable to a random time.

int TIMEOUT = 2000000; // 2 millions micro seconds, which is 2 seconds
int mytimeout = (rand() % TIMEOUT) + 1; // a random time between 0 and 2 seconds
int mysec = mytime/1000000; // the number of seconds
int myMicroSec = mytime -  mysec * 1000000; // the number of micro seconds
static struct timeval poll_interval_tv = {mysec, myMicroSec};

myevent = event_new(libevent_get_base(), -1,EV_TIMEOUT,
                        mycallback_function, arg);

It returns these errors: error: initializer element is not constant error: (near initialization for ‘poll_interval_tv.tv_sec’)

0

There are 0 best solutions below