Important: Please read the Qt Code of Conduct -

[SOLVED] Random Number Generation

  • I've created a very simple random number generator. The code is below. My problem is that it is not random. The seed value is set the current msec() of the current time and, when I run the program I get a list of 10 random values. When I stop the program and wait a few seconds even a couple of minutes before running the program, I receive the same list of random numbers. I was thinking that perhaps the msec() is not being reset but it appears that it is.

    I am at a loss. I'm sure that it's a simple error somewhere between the back of my chair and the keyboard but I can figure out what I'm doing wrong.

    QTime time = QTime::currentTime();

    for (int idx = 0; idx < 10; idx++)
    //int ranNum = mu.Randomizer(0, 100);
    qDebug() << QString::number(((rand() % (101)) + 0));


  • Why are you using qsrand to set the seed but not qrand then?

  • LOL! Do you realize how long I have stared at that stupid line and realized what I was looking at?!?!

    Geez. Perhaps I should go into project management!


    My apologies to any project managers out there.

Log in to reply