更新时间:2018-07-09 17:12:26浏览次数:1+次
#include <iostream>
using namespace std;
#include <time.h>
string get_rand_str(int len, int mode = 0)
{
char base;
int range;
//选择一亩田,准备耕种。选择随机模式:纯数字随机字符串、小写字母随机字符串和大写字母随机字符串
switch (mode)
{
case 0:
{
base = '0';
range = 10;
break;
}
case 1:
{
base = 'a';
range = 26;
break;
}
case 2:
{
base = 'A';
range = 26;
break;
}
}
len++;
srand((int)time(0));//种下一颗种子,等待发芽
unsigned char * p = new unsigned char[len];//开垦一块荒地
memset(p, 0, len);//松好土
for (int i = 0; i < len - 1; i++)//开始播种和灌溉成长
{
p[i] = (unsigned char)(base + (rand() % range));
}
string str((char*)p);//收割丰收的果实
delete p;//清理地块
return str;//背回果实到家,完毕
}
相关资讯