Originally published on the old depletionmode / 2of1 blog (archived copy).
There’s a quick and effective way of producing a random value in C if you only require it at most once per second using current time as a random seed:
#include <time.h> // time_t, time()
#include <stdlib.h> // srand(), rand()
time_t s;
time(&s);
srand((unsigned int)s);
// calc random value (limits are inclusive)
int rand_val = 1 + (int)((float)UPPER_LIMIT *
(rand() / (RAND_MAX + (float)LOWER_LIMIT)));Of course reading from /dev/urandom is good too
(on linux).