C++ 随机生成数字和字母组合的字符串
2015-01-06 11:46
2051 查看
一直学习java,今天突然要用到C++ 写个随机生成数字和字母组合的字符串。。。
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
char *rand_str(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<<rand_str(name,20)<<endl;
system("pause");
return 0;
}
运行结果:
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
char *rand_str(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<<rand_str(name,20)<<endl;
system("pause");
return 0;
}
运行结果:
相关文章推荐
- web前端js中随机生成指定位数的字母数字、组合字符串可选择字母大小写
- c++中生成n个m位的随机不重复字符串的一种方法(字母+数字)
- JS基础篇--通过JS生成由字母与数字组合的随机字符串
- JS实现生成由字母与数字组合的随机字符串功能详解
- selenium webdriver 杂记-生成随机数字字母组合参数
- PHP生成随机字符串(字母+数字)
- js随机生成字母数字组合的字符串 随机动画数字
- php生成随机字符串可指定纯数字、纯字母或者混合的
- RndNum 生成随机字符串,包含数字和小写字母
- 生成6位随机数字字母组合
- 随机生成6位验证码或密码(字母和数字组合)
- JAVA生成8位随机字符串,数字+英文字母
- php 随机生成数字字母组合
- [转]java生成随机数字和字母组合
- php生成随机字符串可指定纯数字、纯字母或者混合的
- js随机生成字母数字组合的字符串 随机动画数字
- ASP生成随机字符串(数字+大小写字母)的代码
- ASP生成随机字符串(数字+大小写字母)的代码
- java生成随机数字和字母组合
- 生成随机字母字符串(数字字母混和)