Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Solved: Question about qrand()

Solved: Question about qrand()

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 17.5k Views
  • 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.
  • R Offline
    R Offline
    romeo.rbw
    wrote on last edited by
    #1

    Hi...
    I make a test of qrand() in my application. I want to make a random integer from 1 to 5. The instruction is:
    qDebug << "ID random: " << qrand() % 5;

    but after tested many times the result always 1. Is there any suggestion? I need your help.
    Thanks in advance.

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      Hi there,
      Do you only call it ones after program startup?
      Then it's the same as the default srand() function in C. It will always hold the same value when not initialised with a value. So you could use the msec time expired since program stated to set the initialised value. That should give you a more random value.
      Greetz

      Greetz, Jeroen

      1 Reply Last reply
      0
      • R Offline
        R Offline
        romeo.rbw
        wrote on last edited by
        #3

        Thank for reply. Yes, I only call once in the beginning of Start button action. I hope can catch a random value for the next processing. Still confusing...:)

        1 Reply Last reply
        0
        • R Offline
          R Offline
          romeo.rbw
          wrote on last edited by
          #4

          Finally it works, I used srand() in Qt qsrand() and using the code:

          qsrand(time(NULL));
          ID=qrand() % 6;
          qDebug << “ID random: “ << ID;

          Thanks for the idea srand().

          1 Reply Last reply
          0

          • Login

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