产生随机数

来源:互联网 发布:姚明新秀数据 编辑:程序博客网 时间:2024/05/13 06:45
// 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:3if(n<=3){c=(rand()%10+'0');str[i]=c;}else{//进一步判断生成的随机字符是大写还是小写,比例为1:1n1=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;}

0 0