Skip to content

Instantly share code, notes, and snippets.

@foxoman
Forked from fffaraz/rand.cpp
Created May 5, 2018 07:13
Show Gist options
  • Save foxoman/1f091f684753d343188d3dc7159eac27 to your computer and use it in GitHub Desktop.
Save foxoman/1f091f684753d343188d3dc7159eac27 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment