Results 1 to 3 of 3

Thread: Problem with qsrand and time(0)[SOLVED]

  1. #1
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Problem with qsrand and time(0)[SOLVED]

    At least, there is where I think I'm having a problem.

    I have the next code:

    Qt Code:
    1. 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};
    2. qsrand(time(0));
    3. int iRandomColor = (qrand() % 11);
    4. QColor myColor = vColor[iRandomColor];
    To copy to clipboard, switch view to plain text mode 

    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!
    Last edited by jano_alex_es; 17th July 2009 at 08:42.

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with qsrand and time(0)

    try qsrand again before creating another object

  3. The following user says thank you to nish for this useful post:

    jano_alex_es (17th July 2009)

  4. #3
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Problem with qsrand and time(0)

    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!

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.