Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [SOLVED] Random Number Generation
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Random Number Generation

Scheduled Pinned Locked Moved C++ Gurus
3 Posts 2 Posters 23.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cazador7907
    wrote on 11 Feb 2011, 19:07 last edited by
    #1

    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();
    qsrand((uint)time.msec());

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

    @

    Laurence -

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on 11 Feb 2011, 19:46 last edited by
      #2

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

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cazador7907
        wrote on 11 Feb 2011, 20:17 last edited by
        #3

        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.

        Laurence -

        1 Reply Last reply
        0

        1/3

        11 Feb 2011, 19:07

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved