标签:数组 details get csdn 组成 rand blog break case
这里的每个数字被选取的几率大于每个字母被选取的几率,但小写字母、大写字母、数字三类的几率一样,
要改善这个问题我觉得可以开一个62大小的字符数组,然后随机数直接mod62填写该下标对应的字符
#include <iostream> #include <stdlib.h> #include <time.h> using namespace std; char *randstr(char *str, const int len) { srand(time(NULL)); int i; for (i = 0; i < len; ++i) { switch ((rand() % 3)) { case 1: str[i] = ‘A‘ + rand() % 26; break; case 2: str[i] = ‘a‘ + rand() % 26; break; default: str[i] = ‘0‘ + rand() % 10; break; } } str[++i] = ‘\0‘; return str; } int main(void) { char name[20]; cout << randstr(name, 8) << endl; system("pause"); return 0; }
标签:数组 details get csdn 组成 rand blog break case
原文地址:https://www.cnblogs.com/exciting/p/11172237.html