Created
July 22, 2014 10:51
-
-
Save fffaraz/b48dff1b4d23afe1573e to your computer and use it in GitHub Desktop.
Random number in Qt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QGlobal.h> | |
#include <QTime> | |
int QMyClass::randInt(int low, int high) | |
{ | |
// Random number between low and high | |
return qrand() % ((high + 1) - low) + low; | |
} | |
// Create seed for the random | |
// That is needed only once on application startup | |
QTime time = QTime::currentTime(); | |
qsrand((uint)time.msec()); | |
//qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | |
// Get random value between 0-100 | |
int randomValue = randInt(0,100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or you can use
QRandomGenerator::global()->generate()
since Qt 5.10.