// random.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
char str[5];//获取五位的随机串
srand((int)time(NULL));//获得随机种子.
int n;//用来随机判断随机串的某一位是数字还是字母
int n1;//用来进一步判断究竟是生成大写字母还是小写字母.
char c;//用来获取随机字符
int num=0;
while(num<1)
{
for(int i=0;i<5;i++)
{
n=(rand()%10+1);
//人工设定字符串是字母或者数字的比例为7:3
if(n<=3)
{
c=(rand()%10+'0');
str[i]=c;
}
else
{
//进一步判断生成的随机字符是大写还是小写,比例为1:1
n1=rand()%2+1;
if(n1==1)
{
c=(rand()%26+'a');
str[i]=c;
}
else
{
c=(rand()%26+'A');
str[i]=c;
}
}
}
printf("本次验证码生成的结果是:");
int i;
for(i=0;i<5;i++)
{
printf("%c",str[i]);
}
printf("\n");
num++;
}
getchar();
return 0;
}
产生随机数
最新推荐文章于 2024-02-18 22:59:30 发布