C語言產生隨機字串
阿新 • • 發佈:2019-02-07
//產生長度為length的隨機字串 char* genRandomString(int length) { int flag, i; char* string; srand((unsigned) time(NULL )); if ((string = (char*) myMalloc(length)) == NULL ) { myLog("Malloc failed!flag:14\n"); return NULL ; } for (i = 0; i < length - 1; i++) { flag = rand() % 3; switch (flag) { case 0: string[i] = 'A' + rand() % 26; break; case 1: string[i] = 'a' + rand() % 26; break; case 2: string[i] = '0' + rand() % 10; break; default: string[i] = 'x'; break; } } string[length - 1] = '\0'; return string; }