PDA

View Full Version : Problem with qsrand and time(0)[SOLVED]



jano_alex_es
17th July 2009, 09:23
At least, there is where I think I'm having a problem.

I have the next code:


QColor vColor[11] = {Qt::black, Qt::red, Qt::darkRed, Qt::darkGreen, Qt::blue, Qt::darkBlue, Qt::darkCyan, Qt::darkMagenta, Qt::yellow, Qt::darkYellow, Qt::darkGray};
qsrand(time(0));
int iRandomColor = (qrand() % 11);
QColor myColor = vColor[iRandomColor];

This is a part of a function called by the constructor of a class, and I create two objects one after the other. But they always have the same color. BUT if I stop it (breakpoint or "Sleep"), the color is different.

I think this could be because the qsrand is always choosing the same value, because it's executed at the same time (or pretty close), that's why it chooses a different color when I stop the execution.

But I'm not sure... and, anyway, I don't know how I could solve that.

any idea? thanks!

nish
17th July 2009, 09:26
try qsrand again before creating another object

jano_alex_es
17th July 2009, 09:41
yes, it was something like that :)

I just seed the time at the beginning of the code, and get the random number later with qrand when necessary.

thanks!