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. random number
QtWS25 Last Chance

random number

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 448 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.
  • V Offline
    V Offline
    veysel.olgun
    wrote on last edited by veysel.olgun
    #1

    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

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • V 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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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
      2
      • V 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

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @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


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

        1 Reply Last reply
        1
        • V Offline
          V Offline
          veysel.olgun
          wrote on last edited by
          #4

          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
          0

          • Login

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