Results 1 to 6 of 6

Thread: Some issues with qrand.

  1. #1
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Some issues with qrand.

    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/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.
    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.

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Some issues with qrand.

    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.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Some issues with qrand.

    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

  4. The following user says thank you to BalaQT for this useful post:

    def (26th November 2010)

  5. #4
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Some issues with qrand.

    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.
    Last edited by def; 26th November 2010 at 10:12.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Some issues with qrand.

    Quote Originally Posted by def View Post
    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:
    Qt Code:
    1. qsrand(QDateTime::currentMSecsSinceEpoch ())
    To copy to clipboard, switch view to plain text mode 
    or better yet something like:
    Qt Code:
    1. 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #6
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Some issues with qrand.

    You could always use QUuid if you really need a unique seed.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

Similar Threads

  1. Two problems with the function qrand()
    By Trader in forum Qt Programming
    Replies: 7
    Last Post: 5th November 2010, 22:01
  2. Some QTreeView Issues
    By kazibi in forum Newbie
    Replies: 0
    Last Post: 27th December 2009, 14:51
  3. 4.6.0 issues on OS X 10.6 with GL
    By treaves in forum Qt Programming
    Replies: 2
    Last Post: 2nd December 2009, 17:12
  4. QTableView Issues with Qt 4.5.3
    By tntcoda in forum Qt Programming
    Replies: 0
    Last Post: 5th October 2009, 20:58
  5. QTextBrowser Issues
    By anju123 in forum Qt Programming
    Replies: 0
    Last Post: 20th July 2007, 05:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.