LKBEN11614: How to generate a random number between two boundaries in C++ with Qt.


Symptom

You need a good random number with a low and a high boundary.

Cause

-

Solution

You will need to include the headerfile QRandomGenerator64.

Please note, this is not a complete example and it will not compile on its own!

#include <QRandomGenerator64>
...

//generate a random number between 1000 and 9999
quint32 challenge = QRandomGenerator::global()->bounded(1000, 9999);
...

//show the random number in a edit field
ui->lineEditChallenge->setText(QString::number(challenge));

 

Have fun.

Disclaimer:

The information provided in this document is intended for your information only. Lubby makes no claims to the validity of this information. Use of this information is at own risk!

About the Author

Author: Wim Peeters - Keskon GmbH & Co. KG

Wim Peeters is electronics engineer with an additional master in IT and over 30 years of experience, including time spent in support, development, consulting, training and database administration. Wim has worked with SQL Server since version 6.5. He has developed in C/C++, Java and C# on Windows and Linux. He writes knowledge base articles to solve IT problems and publishes them on the Lubby Knowledge Platform.

Latest update: 08/12/2022