PDA

View Full Version : Some issues with qrand.



def
24th November 2010, 00:12
1. If I seach for documentation on qrand it says there isn't any. (Type qrand in the search box - top left - on this page: http://doc.qt.nokia.com/4.7). Yet there is some on this page:
http://doc.qt.nokia.com/4.7/qtglobal.html
Is this a defect on the documentation pages? If so where should I report it?
2. In the example application:
http://doc.qt.nokia.com/latest/threads-queuedcustomtype.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.
3. In the same example the seed is set in the main thread, but the random numbers are generated in a 2nd thread. This 2nd thread is stop and started many times when running the application - each time it is started the same set of random numbers are generated. Thus this example application needs to set the seed in the 2nd thread not the main thread.

franz
24th November 2010, 09:20
You might want to search through the bug tracker if such an issue is already known. If not, you can create one. Maybe even send in a patch or merge request.

BalaQT
25th November 2010, 07:19
It seems QtGlobal is not added in online documentation's search tool.

here is the link to report bug:
http://bugreports.qt.nokia.com/secure/Dashboard.jspa
it reqs login.



bala

def
26th November 2010, 10:12
Thanks. I've posted two defects about this now:
http://bugreports.qt.nokia.com/browse/QTWEBSITE-109
http://bugreports.qt.nokia.com/browse/QTWEBSITE-110

Added after 7 minutes:

In responce to franz comment:
> Maybe even send in a patch or merge request.
If I were to patch the example then I would need to be absolutely sure I was right. And currently:
- I'm not absolutely sure what this:
QTime::currentTime().elapsed()
should return in the example program - it is always returning 0, but the defect could be in this function which for all I know should be returning something different.
- I've not found anything in the documentation which says that each thread is independant from the point of view of random number generation or not. Thus I could move the seed setting into the 2nd thread and submit that as a code change, but it maybe that it is the wrong thing to do.

wysota
26th November 2010, 10:17
2. In the example application:
http://doc.qt.nokia.com/latest/threads-queuedcustomtype.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 ())
or better yet something like:

qsrand(QDateTime::currentMSecsSinceEpoch () % 1000000)

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.

franz
26th November 2010, 19:57
You could always use QUuid if you really need a unique seed.