Qt 隨機數(shù)
相關(guān)函數(shù)
qsrand(unsigned seed);
qrand();
以上為偽隨機數(shù)[1],不同在于qsrand()
可以根據(jù)種子值seed的值不同贴谎,來生成不同的隨機數(shù)序列汞扎。因此,可以使用當(dāng)前時間作為種子擅这,來進行模擬隨機數(shù)澈魄。
示例代碼:
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()
的值。
-
條件相同的情況下仲翎,函數(shù)運行兩次產(chǎn)生的隨機序列一致痹扇。 ?