随即生成英文字母

来源:百度知道 编辑:UC知道 时间:2024/05/06 05:59:22
如何随即生成0-9的数字和26位英文字母
比如说设置5位数
随即生成 可能是数字也可能是英文字母的该怎么 写?

// 设定随机数发生公式的种子值
srand( (unsigned)time( NULL ) );

// 获得0-9随机数
int i = rand() % 10;

//随机字母小写
char ch = rand()%26+97;

//随机字母大写
char ch = rand()%26+65;

//////////////////////////////
一位一位随机生成

Code_nst = "1234567890"
Recode_nst = RoundStr(Code_nst,8)
Function RoundStr(str,Num)
s = ""
for i = 1 to Num
Randomize
strLen = Len(str)
t = Round((Rnd * (strLen-1))+1)
s = s & mid(str,t,1)
Next
RoundStr = s
End Function

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

char nlGen()
{

static char data[] =
{
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
};

return data[rand() % (sizeof data - 2)];
}

int main()
{
string Gened;