views:

64

answers:

4

Hi there

I have a QT app, running 2 more threads.

Inside the threads I use the qrand function to generate a random number. The following is the code used to get the number, where m_fluctuations max is a double.

int fluctuate =  qrand() % (int)(m_FluctuationMax * 100);

I tried adding the following code in the main thread, and also inside the thread classes.

QTime now = QTime::currentTime();
qsrand(now.msec());

Now the problem is, that the values being generated are always the same, each time the application is started.

Shouldn't they be different, since the seed is set by 'currentTime()'.

Thanks

+1  A: 

The first thing I'd be checking is the value of now.msec(). It only returns the millisecond pard of the current time and the doco states:

Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.

It may be that your platform always returns the same value for msec(). If that's the case, you could try using minutes and seconds combined somehow (assuming you're not running your code multiple times every second).

You haven't stated which platform you're running on but the Qt source code only supports sub-second resolution if either Q_OS_WIN or Q_OS_UNIX is set.


Keep in mind that the random numbers are per-thread so you should probably do the qsrand in each thread, lest it be automatically seeded with 1.

paxdiablo
I checked that now and the value of msec is different ever time, but the random number is the same every time.
Michael Frey
@Michael, is the _number_ the same every time you call `qrand()` or is the _sequence_ the same (with different numbers from each `qrand()` call) on each program run?
paxdiablo
Also what is m_Fluctuation_Max set to? And, if you're always getting the same number, what is it?
paxdiablo
The sequence is the same each time the app is run. I found the solution though. Check the answer below.
Michael Frey
A: 

Hey,

I think you'll find the answer here : http://stackoverflow.com/questions/2767383/use-of-qsrand-random-method-that-is-not-random

Hope it helps !

Andy M
Not sure about that. OP seems to indicate that same sequence occurs on different _runs_ of the program, not getting the exact same number due to multiple seeding in a single millisecond. I may be wrong, it wouldn't be the first time :-)
paxdiablo
No, that was also not it, but I started to investigate where I call qsrand, and found the solution.
Michael Frey
A: 

I had my qsrand() in the thread/class constructor. When i moved it to the run() function, it started to work randomly. Not sure why it would not work from the constructor though. Thanks everyone for your help.

Michael Frey
A: 

I had my qsrand() in the thread/class constructor. When i moved it to the run() function, it started to work randomly. Not sure why it would not work from the constructor though.

qsrand() uses thread-local storage to store the seed which is actually the pseudorandom number generator state that also gets updated on each call to qrand(). If you seed the PRNG outside the thread where you will be using it, that seed does not influence the outcome. Thread-local storage usually defaults to zero so that way you would get the same sequence of pseudorandoms every time because the seed is always the same.

laalto