PDA

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



jimbo
6th November 2015, 19:46
Hello,

I'm using the code shown to generate random numbers.


timeval ti;

gettimeofday(&ti, NULL);
qsrand(qsrand((long long)ti.tv_usec * (long long)ti.tv_sec);
qDebug() << (long long)((long long)ti.tv_usec * (long long)ti.tv_sec);

num = (rand() % maxNum + 1);


1151813750172254
1152103117531054
1020841750830240
1033346761335855

Is there any difference to the result in using:-

qsrand((long long)ti.tv_usec * (long long)ti.tv_sec);
against

qsrand(ti.tv_usec * ti.tv_sec);

Both seem to give acceptable results.

Regards

ChrisW67
6th November 2015, 20:47
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.

jimbo
7th November 2015, 09:49
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