当前位置:网站首页>QT generate random numbers (random strings) within the specified range
QT generate random numbers (random strings) within the specified range
2022-06-25 17:50:00 【Autodesk_ Glodon】
1. Purpose
Writing QT Project time , Generating random numbers and random strings is a very common requirement , Use qrand Function can realize , But if you don't use seeds , The resulting random number is not a real random number , It's a fixed number . Usually , We will use the current time as the seed of random numbers , To produce real random numbers
2. Generate a random number in a specified range
Qt The code for generating random numbers is as follows :
int getRandom(int min,int max)
{
qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
int num = qrand()%(max-min);
qDebug()<<num+min;
}
3. Generate a random array that does not repeat
Store the generated random number in the array , Every time a new random number is generated , Check whether the number is included in the random number sequence table
If included, regenerate , Until a new random number is generated .
int getRandom(int min,int max)
{
QList<int> list;
int i,j;
bool res = true;
qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
for (int i=0; i<10; i++) {
int num = qrand()%(max-min)+min;
list.append(num);
while(res)
{
for(j=0;j<i;j++)
{
if(list[i]==list[j])
break;
}
if(j<i)
{
list[i] = qrand()%(max-min)+min;
}
if(j==i)
res=!res;
}
}
}
4. Generate random strings
The random string generation code is as follows :
QString getRandomString(int length)
{
qsrand(QDateTime::currentMSecsSinceEpoch());
const char ch[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int size = sizeof(ch);
char* str = new char[length + 1];
int num = 0;
for (int i = 0; i < length; ++i)
{
num = rand() % (size - 1);
str[i] = ch[num];
}
QString res(str);
return res;
}
边栏推荐
- RuntimeError: Trying to backward through the graph a second time (or directly access saved variable
- Unity technical manual - size over lifetime and size by speed
- 杰理之系统时钟设置出现复位或无效问题【篇】
- 股票开户怎么办理 办理开户安全吗
- Which of the top ten securities companies has the lowest commission? Is it safe to open an account
- Use of jupyter
- 启牛的涨乐财付通如何?安全靠谱吗
- 华为云GaussDB(for Redis)揭秘第19期:GaussDB(for Redis)全面对比Codis
- Precautions for using timer_cap.c of Jerry [chapter]
- [matlab] data interpolation
猜你喜欢
BILSTM和CRF的那些事
ASP.NET超市便利店在线购物商城源码,针对周边配送系统
ACY100油烟浓度在线监控仪针对饮食业厨房油烟排放
Uncover ges super large scale graph computing engine hyg: Graph Segmentation
Qinheng ch583 USB custom hid debugging record
使用DiskGenius拓展系统盘C盘的容量
DDD concept is complex and difficult to understand. How to design code implementation model in practice?
Virtual machine class loading mechanism
移动端异构运算技术 - GPU OpenCL 编程(基础篇)
杰理之如何给外界输出一个时钟源使用【篇】
随机推荐
ACY100油烟浓度在线监控仪针对饮食业厨房油烟排放
New characteristics of cultural consumption in the era of digital economy
SDN system method | 9 Access network
Operating steps for installing CUDA in win10 (continuous improvement)
DDD concept is complex and difficult to understand. How to design code implementation model in practice?
Introduction to the container of() function
Encryption trend: Fashion advances to the meta universe
Jerry's system clock setting is reset or invalid [chapter]
利用Qt制作美化登录界面框
Kotlin of Android cultivation manual - several ways to write a custom view
杰理之获取复位源和唤醒的 IO 口的方法【篇】
Qt产生指定范围内随机数(随机字符串)
Hover effect of eight buttons
HMS Core机器学习服务实现同声传译,支持中英文互译和多种音色语音播报
用连续自然数之和来表达整数
相同wifi下,笔记本连接台式机上的虚拟机
SDN系统方法 | 9. 接入网
杰理之唤醒口使用注意事项【篇】
Utilisation de diskgenius pour augmenter la capacité du disque système C
Indexes