
Originally Posted by
def
2. In the example application:
http://doc.qt.nokia.com/latest/threa...ustomtype.html
the random seed is set in main() using:
QTime::currentTime().elapsed();
but this is always 0 when I run on windows. Having looked at the documentation I can't see why this is always zero unless the framework calls start() during booting an application - if it does then this example needs changing to use a better seed.
This will always be zero. A temporary QTime object is created and its elapsed() is queried immediately which is bound to be 0 because the time object was never started. It would be much better if the code was substituted with:
qsrand(QDateTime::currentMSecsSinceEpoch ())
To copy to clipboard, switch view to plain text mode
or better yet something like:
qsrand
(QDateTime::currentMSecsSinceEpoch () % 1000000)
qsrand(QDateTime::currentMSecsSinceEpoch () % 1000000)
To copy to clipboard, switch view to plain text mode
In general bear in mind rand() and srand() are quite poor when it comes to generating random numbers and if you really want randomness, you should use some better random number generating algorithm. You can see that even in my last piece of code there is a 1E-6 chance (which is quite high) of getting two identical seeds in a row.
Bookmarks