产生随机数

// 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值