Qt 随机数

2016-03-31  本文已影响0人  厝弧

Qt 随机数

相关函数

qsrand(unsigned seed);
qrand(); 

以上为伪随机数[1],不同在于qsrand()可以根据种子值seed的值不同,来生成不同的随机数序列。因此,可以使用当前时间作为种子,来进行模拟随机数。
示例代码:

qsrand(QTime(0,0,0).secsTo(QTime::currentTime()))

上面的代码中QDateTime::secsTo()解释如下:

  • Returns the number of seconds from this time to t. If t is earlier than this time, the number of seconds returned is negative.

实际上QTime(0,0,0).secsTo(QTime::currentTime())返回的值就是从0到QTime::currentTime()的值。


  1. 条件相同的情况下,函数运行两次产生的随机序列一致。

上一篇 下一篇

猜你喜欢

热点阅读