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. identifier qrand is undefined
Forum Updated to NodeBB v4.3 + New Features

identifier qrand is undefined

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 3 Posters 2.0k 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.
  • R Roberrt

    Im trying to update this project: https://github.com/omkar-developer/QtCaptcha

    But when i replace rand to QRandomGenerator::global() i get many compile errors

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #7

    @Roberrt Always look at the first error.

    1 Reply Last reply
    0
    • R Roberrt

      Im trying to update this project: https://github.com/omkar-developer/QtCaptcha

      But when i replace rand to QRandomGenerator::global() i get many compile errors

      Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #8

      @Roberrt Please please read that page I linked. It's the least you can do.
      QRandomGenerator::global() returns an instance of the generator. To get a number call
      QRandomGenerator::global()->generate()

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Roberrt
        wrote on last edited by
        #9

        @Chris-Kawa said in identifier qrand is undefined:

        QRandomGenerator::global()->generate()

        Im already using QRandomGenerator::global()->generate() it was just a typo

        Chris KawaC 1 Reply Last reply
        0
        • R Roberrt

          @Chris-Kawa said in identifier qrand is undefined:

          QRandomGenerator::global()->generate()

          Im already using QRandomGenerator::global()->generate() it was just a typo

          Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #10

          @Roberrt Then what's the error?

          R 1 Reply Last reply
          0
          • Chris KawaC Chris Kawa

            @Roberrt Then what's the error?

            R Offline
            R Offline
            Roberrt
            wrote on last edited by Roberrt
            #11

            @Chris-Kawa i've been able to fix some, but this one don't:

            https://github.com/omkar-developer/QtCaptcha/blob/6356446e4864bcb7acfd332165f0073a247c71a0/captcha.cpp#L455

            text += chars[(qrand() / (qreal) RAND_MAX) * (chars.size() - 1.0)];
            

            to

            text += chars[(QRandomGenerator::global()->generate() / (qreal) RAND_MAX) * (chars.size() - 1.0)];
            

            Now im getting this error: no operator "+=" matches these operands

            I don't know if replacing these rands messed something up in the code, I'm getting a different layout than in her GitHub pictures

            probably I shouldn't remove this line as @JonB suggested

            void Captcha::randomize()
            {
                //qsrand(QTime::currentTime().msec());
            }
            

            55c0f023-e85c-462e-95cb-54312c23c018-image.png

            JonBJ 1 Reply Last reply
            0
            • R Roberrt

              @Chris-Kawa i've been able to fix some, but this one don't:

              https://github.com/omkar-developer/QtCaptcha/blob/6356446e4864bcb7acfd332165f0073a247c71a0/captcha.cpp#L455

              text += chars[(qrand() / (qreal) RAND_MAX) * (chars.size() - 1.0)];
              

              to

              text += chars[(QRandomGenerator::global()->generate() / (qreal) RAND_MAX) * (chars.size() - 1.0)];
              

              Now im getting this error: no operator "+=" matches these operands

              I don't know if replacing these rands messed something up in the code, I'm getting a different layout than in her GitHub pictures

              probably I shouldn't remove this line as @JonB suggested

              void Captcha::randomize()
              {
                  //qsrand(QTime::currentTime().msec());
              }
              

              55c0f023-e85c-462e-95cb-54312c23c018-image.png

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #12

              @Roberrt
              qsrand() seeds for qrand(). It has nothing to do with QRandomGenerator.

              RAND_MAX is also for qrand(). Just casting it to qreal does not mean it's suitable to use with QRandomGenerator.

              Why are you indexing an array by a qreal expression?

              R 1 Reply Last reply
              0
              • JonBJ JonB

                @Roberrt
                qsrand() seeds for qrand(). It has nothing to do with QRandomGenerator.

                RAND_MAX is also for qrand(). Just casting it to qreal does not mean it's suitable to use with QRandomGenerator.

                Why are you indexing an array by a qreal expression?

                R Offline
                R Offline
                Roberrt
                wrote on last edited by
                #13

                @JonB This project is not mine, im just trying to use it, but got these qrand errors

                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by Chris Kawa
                  #14

                  This expression (qrand() / (qreal) RAND_MAX) gives you a random double number in range 0..1.
                  So you can just replace this whole thing with QRandomGenerator::global()->generateDouble(), which does the same. Same for the lines above it.
                  Or better yet - remove that whole expression with bounded() which will give you a random index in a range and you won't have to do any of those shenanigans.

                  Now im getting this error: no operator "+=" matches these operands

                  text is a QString. chars[x] is an unsigned char. Convert it to QChar. Also convert the expression in [] to an integer.

                  probably I shouldn't remove this line as @JonB suggested

                  If there's a function that seeds the generator and you want to preserve it then you can use QRandomGenerator::seed, but it's not necessary.

                  R 1 Reply Last reply
                  1
                  • Chris KawaC Chris Kawa

                    This expression (qrand() / (qreal) RAND_MAX) gives you a random double number in range 0..1.
                    So you can just replace this whole thing with QRandomGenerator::global()->generateDouble(), which does the same. Same for the lines above it.
                    Or better yet - remove that whole expression with bounded() which will give you a random index in a range and you won't have to do any of those shenanigans.

                    Now im getting this error: no operator "+=" matches these operands

                    text is a QString. chars[x] is an unsigned char. Convert it to QChar. Also convert the expression in [] to an integer.

                    probably I shouldn't remove this line as @JonB suggested

                    If there's a function that seeds the generator and you want to preserve it then you can use QRandomGenerator::seed, but it's not necessary.

                    R Offline
                    R Offline
                    Roberrt
                    wrote on last edited by
                    #15

                    @Chris-Kawa thanks, changing it to QRandomGenerator::global()->generateDouble() fixed the widget not being show correctly.

                    text += QChar(chars[(QRandomGenerator::global()->generate() / (qreal) RAND_MAX)  * (chars.size() - 1.0)]);
                    

                    I'm getting an exception index out out of range

                    Chris KawaC 1 Reply Last reply
                    0
                    • R Roberrt

                      @Chris-Kawa thanks, changing it to QRandomGenerator::global()->generateDouble() fixed the widget not being show correctly.

                      text += QChar(chars[(QRandomGenerator::global()->generate() / (qreal) RAND_MAX)  * (chars.size() - 1.0)]);
                      

                      I'm getting an exception index out out of range

                      Chris KawaC Offline
                      Chris KawaC Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on last edited by
                      #16

                      @Roberrt said in identifier qrand is undefined:

                      (QRandomGenerator::global()->generate() / (qreal) RAND_MAX) * (chars.size() - 1.0)

                      Replace this whole thing with

                      QRandomGenerator::global()->bounded(chars.size())
                      
                      1 Reply Last reply
                      2

                      • Login

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