当前位置:网站首页>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;
}
边栏推荐
- 【工作小技巧】刚入职的软件测试工程师怎么快速上手新岗位
- Langage d'assemblage (5) Registre (accès à la mémoire)
- 使用DiskGenius拓展系统盘C盘的容量
- Accumulation of some common knowledge points
- 有关均衡----简易版瓶颈模型
- Under the same WiFi, the notebook is connected to the virtual machine on the desktop
- Hover effect of eight buttons
- How to solve the problem of network disconnection after enabling hotspot sharing in win10?
- Which of the top ten securities companies has the lowest commission? Is it safe to open an account
- golang list to string
猜你喜欢

深入理解ELF文件

杰理之增加加密文件播放功能【篇】

Introduction to the container of() function

ASP.NET超市便利店在线购物商城源码,针对周边配送系统

SDN system method | 9 Access network

Assembly language (5) register (memory access)

近来开发的一款简单易用的图可视化工具
![[UVM practice== > episode_2] ~ VIP, VIP development, VIP release](/img/de/4befb321d5cf5b0aa630e9ad0fda2a.png)
[UVM practice== > episode_2] ~ VIP, VIP development, VIP release

ACY100油烟浓度在线监控仪针对饮食业厨房油烟排放

CVPR small target detection: context and attention mechanisms improve small target detection (attached with paper Download)
随机推荐
The Stackies 2022:32个营销技术栈入选
Agent white paper - jointly build agents and create the wisdom of the whole scene | cloud library No.21 recommendation
HMS Core机器学习服务实现同声传译,支持中英文互译和多种音色语音播报
golang sort slice int
十大龙头券商 办理开户安全吗
WARNING: Unsupported upgrade request.
About queryinterface functions
bert之我的小总结
Accumulation of some common knowledge points
New characteristics of cultural consumption in the era of digital economy
Assembly language (5) register (memory access)
Is Guotai Junan Securities reliable? Is it legal? Is it safe to open a stock account?
Jerry's ADC_ get_ Incorrect voltage value obtained by voltage function [chapter]
Unity technical manual - lifecycle rotation rotationoverlifetime speed rotation rotationbyspeed external forces
利用Qt制作美化登录界面框
启牛涨乐财付通下载是可以开户吗?开户安全吗
Video production material website arrangement
喜报|海泰方圆通过CMMI-3资质认证,研发能力获国际认可
mysql mysql-8.0.19-winx64 安装与navicat连接
C语言中%含义