Qt中使用随机数

Posted 徐岩

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt中使用随机数相关的知识,希望对你有一定的参考价值。

新建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;
}

 

以上是关于Qt中使用随机数的主要内容,如果未能解决你的问题,请参考以下文章

26.Qt Quick QML-RotationAnimationPathAnimationSmoothedAnimationBehaviorPauseAnimationSequential(代码片段

播放随机声音而不重复

Qt中使用随机数

关于qt随机值传递给label问题

QT产生随机数的问题

正在渲染opengl的随机屏幕片段-这表明啥错误?