//随机产生长度不少于6位 包含大写、小写、数字、特殊字符至少三种的密码
string randpass(int n)
{
string res="";
if(n<6)
{
cout<<"长度不能少于6位"<<endl;
return res;
}
int count=0;
int lownum=0,highnum=0,spnum=0,num=0;//分别存小写、大写、特殊字符、数字个数
srand(time(NULL));
while(!(lownum&&highnum&&(spnum||num)||(lownum||highnum)&&num&&spnum))
{
num=rand()%(n-2);
lownum=rand()%(n-num-1);
highnum=rand()%(n-lownum-num);
spnum=n-num-highnum-lownum;
}
int select = -1;
int i=0;
while(i<n)
{
select = rand()%4;
switch(select)
{
case 0:
if(num<=0)break;
res+=rand()%10+'0';
num--;
i++;
break;
case 1:
if(lownum<=0)break;
res+=rand()%26+'a';
lownum--;
i++;
break;
case 2:
if(highnum<=0)break;
res+=rand()%26+'A';
highnum--;
i++;
break;
case 3:
if(spnum<=0)break;
res+=rand()%('/'-'('+1)+'(';
spnum--;
i++;
break;
}
}
cout<<res<<endl;
return res;
}
生成不少于6位的随机密码,生成的随机密码必须至少包含大写字母、小写字母、数字、特殊字符四种中任意三种。
参考了:https://blog.csdn.net/liwei1205/article/details/78071861