Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved random number

    General and Desktop
    3
    4
    308
    Loading More Posts
    • 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.
    • V
      veysel.olgun last edited by veysel.olgun

      hello friend, why random generator always show same number ?```
      for(int j=1;j<s2+1;j++)
      {

          qsrand(time(NULL));
      

      dizi[j]=(qrand()%32);

      }
      

      output :
      3
      3
      3
      3
      or
      28
      28
      28
      28

      jsulm J.Hilk 2 Replies Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @veysel.olgun last edited by

        @veysel.olgun said in random number:

        qsrand

        You're using deprecated functions. You should rather use http://doc.qt.io/qt-5/qrandomgenerator.html

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 2
        • J.Hilk
          J.Hilk Moderators @veysel.olgun last edited by

          @veysel.olgun said in random number:

          qsrand(time(NULL));

          seeds rand with the seconds since the epoch.

          I would think you end up seeding again in a much quicker time than 1 sec -> next rand is again seeded and calculated the same way -> same "random"-number

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply Reply Quote 1
          • V
            veysel.olgun last edited by

            i understand now , when i write

                  qsrand(time(NULL));
              dizi[j]=(QRandomGenerator::global()->generate()%32);
            

            it works fine

            thank you all

            1 Reply Last reply Reply Quote 0
            • First post
              Last post