Sometimes I need to generate a random string to use as unique ID for various use. After some test I found this solution:

void randomString(char *s, const int len) {
    static const char alphanum[] =
            "0123456789"
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    srand(time(NULL) ^ (getpid()<<16));
    int i;
    for (i = 0; i < (len-1); ++i) {
        int index = rand() % (sizeof(alphanum) - 1);
        s[i] = alphanum[index];
    }
    s[len-1] = 0;
}

You need to call that function as:

unsigned char *rdm_string = calloc(10, 1);
randomString(rdm_string, 10);

That way you get a random alphanumeric string of 9 characters and a terminator character ''.