项目记录,分享一个自动生成UID的函数
今天没遇到什么问题,也没啥可记录的,比较顺利。
这个函数是我自己写的,根据不同用户组生成具有不同特征的UID,方便管理:
void randUID(int group, char *newUid)//随机UID,管理员前缀为A,普通用户为U,开发者为D
{
const char *strtoC = nullptr;
char ufrontUID[12] = "U";
char afrontUID[12] = "A";
char dfrontUID[12] = "D";
string behindUID = "\0";
long int randNum = 0;
srand((int)time(0));
randNum = 1000000 + rand() % 100000000;
behindUID = to_string(randNum);
strtoC = behindUID.c_str();
if(group == 1)
{
strcat(afrontUID, strtoC);
strcpy(newUid, afrontUID);
}
else if (group == 3)
{
strcat(ufrontUID, strtoC);
strcpy(newUid, ufrontUID);
}
else
{
strcat(dfrontUID, strtoC);
strcpy(newUid, dfrontUID);
}
}
srand((int)time(0));
randNum = 1000000 + rand() % 100000000;
srand函数给rand函数种子,借助time函数,就可以刷新种子,这样rand函数的种子将会改变,才不会出现伪随机的情况。
注意:需要头文件ctime
。
编辑:Henry 2021-01-11