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. QRandomGenerator ULLONG_MAX
Forum Updated to NodeBB v4.3 + New Features

QRandomGenerator ULLONG_MAX

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 475 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
    Redman
    wrote on last edited by
    #1

    Hello,

    generating a random integer between 0 and ULONGLONG_MAX does not work.

    QRandomGenerator rng;
    //works
       quint16 x = rng.bounded(0, USHRT_MAX);
       QByteArray xHex = QString::number(x, 16).toUpper().toLocal8Bit();
    
    
    // does not work
       quint64 y = rng.bounded(quint64(0), quint64(18446744073709551615));
       quint64 y = rng.bounded(quint64(0), ULLONG_MAX);
       quint64 y = rng.bounded(0, ULLONG_MAX);
       QByteArray yHex = QString::number(y, 16).toUpper().toLocal8Bit();
    
    
    // works
       quint16 z = rng.bounded(0, USHRT_MAX);
       QByteArray zHex = QString::number(z, 16).toUpper().toLocal8Bit();
    

    Anyone know how to make this work?

    Christian EhrlicherC JonBJ 2 Replies Last reply
    0
    • JonBJ JonB

      @Redman
      In what sense "does not work"? Generates a compilation error? Does not return what you expect it to at runtime?

      I was not able to find ULONGLONG_MAX (other than in Boost). Your code does not use such a symbol. ULLONG_MAX does exist and seems to be "unsigned long long int", 64-bit. The (only) overload I see matching this is quint64 QRandomGenerator::bounded(unsigned int lowest, quint64 highest). Make sure you use this overload. I don't see any overload which takes quint64 for both lower & upper bounds. quint64 y = rng.bounded(0, ULLONG_MAX); looks like it should match, try static_cast<>ing the parameters to make sure this one is being used?

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #4

      And you should also read the documentation carefully:

      "Note that this function cannot be used to obtain values in the full 64-bit range of quint64. Instead, use generate64()."

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • R Redman

        Hello,

        generating a random integer between 0 and ULONGLONG_MAX does not work.

        QRandomGenerator rng;
        //works
           quint16 x = rng.bounded(0, USHRT_MAX);
           QByteArray xHex = QString::number(x, 16).toUpper().toLocal8Bit();
        
        
        // does not work
           quint64 y = rng.bounded(quint64(0), quint64(18446744073709551615));
           quint64 y = rng.bounded(quint64(0), ULLONG_MAX);
           quint64 y = rng.bounded(0, ULLONG_MAX);
           QByteArray yHex = QString::number(y, 16).toUpper().toLocal8Bit();
        
        
        // works
           quint16 z = rng.bounded(0, USHRT_MAX);
           QByteArray zHex = QString::number(z, 16).toUpper().toLocal8Bit();
        

        Anyone know how to make this work?

        Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by Christian Ehrlicher
        #2

        @Redman said in QRandomGenerator ULLONG_MAX:

        does not work.

        Apart fromt he fact that the code does not compile this is not an useful error description...

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • R Redman

          Hello,

          generating a random integer between 0 and ULONGLONG_MAX does not work.

          QRandomGenerator rng;
          //works
             quint16 x = rng.bounded(0, USHRT_MAX);
             QByteArray xHex = QString::number(x, 16).toUpper().toLocal8Bit();
          
          
          // does not work
             quint64 y = rng.bounded(quint64(0), quint64(18446744073709551615));
             quint64 y = rng.bounded(quint64(0), ULLONG_MAX);
             quint64 y = rng.bounded(0, ULLONG_MAX);
             QByteArray yHex = QString::number(y, 16).toUpper().toLocal8Bit();
          
          
          // works
             quint16 z = rng.bounded(0, USHRT_MAX);
             QByteArray zHex = QString::number(z, 16).toUpper().toLocal8Bit();
          

          Anyone know how to make this work?

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

          @Redman
          In what sense "does not work"? Generates a compilation error? Does not return what you expect it to at runtime?

          I was not able to find ULONGLONG_MAX (other than in Boost). Your code does not use such a symbol. ULLONG_MAX does exist and seems to be "unsigned long long int", 64-bit. The (only) overload I see matching this is quint64 QRandomGenerator::bounded(unsigned int lowest, quint64 highest). Make sure you use this overload. I don't see any overload which takes quint64 for both lower & upper bounds. quint64 y = rng.bounded(0, ULLONG_MAX); looks like it should match, try static_cast<>ing the parameters to make sure this one is being used?

          Christian EhrlicherC R 2 Replies Last reply
          0
          • JonBJ JonB

            @Redman
            In what sense "does not work"? Generates a compilation error? Does not return what you expect it to at runtime?

            I was not able to find ULONGLONG_MAX (other than in Boost). Your code does not use such a symbol. ULLONG_MAX does exist and seems to be "unsigned long long int", 64-bit. The (only) overload I see matching this is quint64 QRandomGenerator::bounded(unsigned int lowest, quint64 highest). Make sure you use this overload. I don't see any overload which takes quint64 for both lower & upper bounds. quint64 y = rng.bounded(0, ULLONG_MAX); looks like it should match, try static_cast<>ing the parameters to make sure this one is being used?

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            And you should also read the documentation carefully:

            "Note that this function cannot be used to obtain values in the full 64-bit range of quint64. Instead, use generate64()."

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            3
            • JonBJ JonB

              @Redman
              In what sense "does not work"? Generates a compilation error? Does not return what you expect it to at runtime?

              I was not able to find ULONGLONG_MAX (other than in Boost). Your code does not use such a symbol. ULLONG_MAX does exist and seems to be "unsigned long long int", 64-bit. The (only) overload I see matching this is quint64 QRandomGenerator::bounded(unsigned int lowest, quint64 highest). Make sure you use this overload. I don't see any overload which takes quint64 for both lower & upper bounds. quint64 y = rng.bounded(0, ULLONG_MAX); looks like it should match, try static_cast<>ing the parameters to make sure this one is being used?

              R Offline
              R Offline
              Redman
              wrote on last edited by
              #5

              @JonB rng.bounded(unsigned int(0), ULLONG_MAX); compiles successfully. There is aproblem in qrandomh.h inline quint64 QRandomGenerator::bounded(quint64 highest) with the mask.

              But as Christian pointed out, the function is not suited for my usecase. Thanks

              1 Reply Last reply
              0
              • R Redman has marked this topic as solved on

              • Login

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