Results 1 to 3 of 3

Thread: C++/Qt4 linux - casting (ti.tv_usec * ti.tv_sec) to long long

  1. #1
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default C++/Qt4 linux - casting (ti.tv_usec * ti.tv_sec) to long long

    Hello,

    I'm using the code shown to generate random numbers.
    Qt Code:
    1. timeval ti;
    2.  
    3. gettimeofday(&ti, NULL);
    4. qsrand(qsrand((long long)ti.tv_usec * (long long)ti.tv_sec);
    5. qDebug() << (long long)((long long)ti.tv_usec * (long long)ti.tv_sec);
    6.  
    7. num = (rand() % maxNum + 1);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. 1151813750172254
    2. 1152103117531054
    3. 1020841750830240
    4. 1033346761335855
    To copy to clipboard, switch view to plain text mode 

    Is there any difference to the result in using:-
    Qt Code:
    1. qsrand((long long)ti.tv_usec * (long long)ti.tv_sec);
    To copy to clipboard, switch view to plain text mode 
    against
    Qt Code:
    1. qsrand(ti.tv_usec * ti.tv_sec);
    To copy to clipboard, switch view to plain text mode 

    Both seem to give acceptable results.

    Regards

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: C++/Qt4 linux - casting (ti.tv_usec * ti.tv_sec) to long long

    Qsrand() takes a uint argument (often 32 bits) so expanding the components (time_t and suseconds_t, may already be 64 bits) of your multiplication to Long long (often 64 bits) is probably only going to result in a implict downsizing of the result and possible discarding of bits anyway. Since you are not actually interested in the value of the multiplication, just looking to give qsrand() an unpredictable value, either method should be fine.

  3. #3
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++/Qt4 linux - casting (ti.tv_usec * ti.tv_sec) to long long

    Hello ChrisW67,

    Thanks for your reply, it was just curiosity on my part.
    I wanted to see what the seed was like and found that I needed 'long long' to get the qDebug to show
    meaningful results,

    Regards

Similar Threads

  1. Long touch
    By flambert in forum Qt Programming
    Replies: 1
    Last Post: 4th March 2015, 07:12
  2. It was a long journey
    By zlatko in forum General Discussion
    Replies: 0
    Last Post: 9th January 2013, 22:11
  3. qDebug() and long long int on WIN
    By iw2lsi in forum Qt Programming
    Replies: 3
    Last Post: 21st March 2012, 05:47
  4. Replies: 2
    Last Post: 5th October 2010, 08:20
  5. what to do during long calculation?
    By Weilor in forum Qt Programming
    Replies: 4
    Last Post: 14th September 2007, 12:11

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.