Results 1 to 6 of 6

Thread: Some issues with qrand.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


  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 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
  •  
Qt is a trademark of The Qt Company.