#include #include #include int RandomString(char **str, int len) { int rc = 0; /* 1 indicates success */ size_t len_ = len; if(str != NULL) { *str = malloc(len_ + 1); if(*str != NULL) { const char sym[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" "\"%^&*()_-abcdefghijklmnopqrstuvwxyz" "+=[]{}\\|,<.>/?"; /* did I miss any? */ size_t maxsym = sizeof sym / sizeof sym[0] - 1; size_t i = 0; while(i < len_) { int r = maxsym * (rand() / (RAND_MAX + 1.0)); (*str)[i++] = sym[r]; } (*str)[i] = '\0'; rc = 1; } } return rc; } int main(void) { int rc = 0; int maxtrials = 20; char *s = NULL; srand((unsigned)time(0)); while(rc == 0 && maxtrials-- > 0) { if(RandomString(&s, 42)) { printf("%s\n", s); free(s); } } return 0; }