C/linux 产生随机字符串
Posted Malphite
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C/linux 产生随机字符串相关的知识,希望对你有一定的参考价值。
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> char* genRandomString(int length) { int flag, i; char* string; srand((unsigned) time(NULL )); if ((string = (char*) malloc(length)) == NULL ) { //myLog("Malloc failed!flag:14 "); 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] = ‘