随机码生成器[10位字母+数字]

说明

最近工作需要固定写死好几个随机码,自己想的又不够随机,所以搞了个随机码生成器[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位字母+数字]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值