这里的每个数字被选取的几率大于每个字母被选取的几率,但小写字母、大写字母、数字三类的几率一样,
要改善这个问题我觉得可以开一个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; }
原文链接: https://www.cnblogs.com/exciting/p/11172237.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/298612
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!