Skip to content

Instantly share code, notes, and snippets.

@fffaraz
Created July 22, 2014 10:51
Show Gist options
  • Save fffaraz/b48dff1b4d23afe1573e to your computer and use it in GitHub Desktop.
Save fffaraz/b48dff1b4d23afe1573e to your computer and use it in GitHub Desktop.
Random number in Qt
#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);
@RomanPodymov
Copy link

Or you can use QRandomGenerator::global()->generate() since Qt 5.10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment