Qt中使用随机数

Posted on 2016-04-01 22:14  徐岩  阅读(468)  评论(0)    收藏  举报

新建Empty qmake project,命名为UseRand

UseRand.pro

SOURCES += \
    main.cpp

QT += core

main.cpp

#include <QTime>
#include <QTextStream>

const int RAND_POS = 29;
const int DOT_SIZE = 10;

int main()
{
    QTextStream out(stdout);

    QTime time = QTime::currentTime();
    qsrand((uint) time.msec());

    int r = qrand() % RAND_POS;
    int x = (r * DOT_SIZE);

    out << RAND_POS << endl;
    out << x << endl;

    return 0;
}