说明
最近工作需要固定写死好几个随机码,自己想的又不够随机,所以搞了个随机码生成器[10位字母+数字]
rand10bit.cpp
#include "rand10bit.h"
#include "ui_rand10bit.h"
#include"time.h"
Rand10Bit::Rand10Bit(QWidget *parent) :
QWidget(parent),
ui(new Ui::Rand10Bit)
{
ui->setupUi(this);
this->setWindowTitle(QString::fromUtf8("10位字母+数值"));
// 对Label可复制
ui->rand10bitLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
}
Rand10Bit::~Rand10Bit()
{
delete ui;
}
void Rand10Bit::on_createBtn_clicked()
{
char chr[]={
'0','1','2','3','4','5','6','7','8','9','0',
'A','B','C','D','E','F','G','H','I','J','K',
'L','M','N','O','P','Q','R','S','T','U','V',
'W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k',
'l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z'};
srand((unsigned int)time(NULL));
QString tempstring;
char buf[10]={
0};
int idx=0;
for(int i=0;i<10;i++)
{
idx=rand()%62;//chr[0-61]
//sprintf_s(buf,"%c",chr[idx]);
sprintf(buf,"%c",chr[idx]);
tempstring.append(buf);
if(7==i)//确保有数值
{
QRegExp regexp("[a-zA-Z]+[0-9]+");// 字母+数值
if(regexp.exactMatch(tempstring)==false)
{
idx=rand()%10;//0-9
sprintf(buf,"%c",chr[idx]);
tempstring.append(buf);
idx=rand()%52+10;//10-61
sprintf(buf,"%c",chr[idx]);
tempstring.append(buf);
break;
}
}
}
ui->rand10bitLabel->setText(tempstring);
}
ui
结构
效果
文件
有道云:随机码生成器[10位字母+数字]