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.
- Because QTime measures time within a day and there are 86400 seconds in a day, the result is always between -86400 and 86400.
- secsTo() does not take into account any milliseconds.
- Returns 0 if either time is invalid.
实际上QTime(0,0,0).secsTo(QTime::currentTime())
返回的值就是从0到QTime::currentTime()
的值。
-
条件相同的情况下,函数运行两次产生的随机序列一致。 ↩