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. Normalize random() output?
Forum Updated to NodeBB v4.3 + New Features

Normalize random() output?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 1.6k 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.
  • S Offline
    S Offline
    ShylerC
    wrote on last edited by
    #1

    Team,

    Is it possible to normalize the output of random()?
    ie --- I only want to see an output of 8 digits?
    For now - I just see varying lengths on the output.

    Thanks!

    J.HilkJ 1 Reply Last reply
    0
    • S ShylerC

      Team,

      Is it possible to normalize the output of random()?
      ie --- I only want to see an output of 8 digits?
      For now - I just see varying lengths on the output.

      Thanks!

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

      @ShylerC
      It's not really a Qt question, but the answer isn't one either.

      To force a range on your random generator one usualy goes the following way

      outPut = random()%((max- min) + 1) + min; 
      

      for 8 digits min is 10000000 and max is 99999999, however due to the nature of this, it will "reduce" the randomness.


      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.

      JonBJ 1 Reply Last reply
      6
      • S Offline
        S Offline
        ShylerC
        wrote on last edited by
        #3

        Thanks,

        I was looking at using QRandomGenerator too.
        I've gotten them both to output well. Just need to control their range to be purely 8 digits.
        Thank you so much!

        //to illustrate, test function -- QRandomGenerator Class//

        int yum;
        yum=700;

        while (yum < 800){
        QRandomGenerator::global();
        int x = QRandomGenerator::global()->generate64();
        yum = yum += 1;
        qDebug() << x;
        }

        /to illustrate, test function -- random()//
        int yum;
        yum=700;

        while (yum < 800){
        yum = yum += 1;
        x = random()
        qDebug() << x;
        }

        1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @ShylerC
          It's not really a Qt question, but the answer isn't one either.

          To force a range on your random generator one usualy goes the following way

          outPut = random()%((max- min) + 1) + min; 
          

          for 8 digits min is 10000000 and max is 99999999, however due to the nature of this, it will "reduce" the randomness.

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

          @J.Hilk

          outPut = random()%((max- min) + 1) + min;

          I realize this is in a sense OT, but I've always been fascinated by (pseudo-) random number generation. You are using MOD (%) to restrict your range. My understanding is that this approach comes from The Devil if the range is "large" compared to random number return size.

          Random numbers have a random distribution of 0s and 1s in each bit. Suppose the random number generator returns an 8 bit number, 0 to 255. Suppose your range is 250, 0 to 249, so you use % 250. Most numbers will occur once, being equally likely. But for generated random numbers 250 to 255 inclusive, the MOD will return a number 0 to 5 inclusive. So 0 to 5 come out twice as likely to be returned compared to any other number!

          Any comments?

          J.HilkJ 1 Reply Last reply
          1
          • JonBJ JonB

            @J.Hilk

            outPut = random()%((max- min) + 1) + min;

            I realize this is in a sense OT, but I've always been fascinated by (pseudo-) random number generation. You are using MOD (%) to restrict your range. My understanding is that this approach comes from The Devil if the range is "large" compared to random number return size.

            Random numbers have a random distribution of 0s and 1s in each bit. Suppose the random number generator returns an 8 bit number, 0 to 255. Suppose your range is 250, 0 to 249, so you use % 250. Most numbers will occur once, being equally likely. But for generated random numbers 250 to 255 inclusive, the MOD will return a number 0 to 5 inclusive. So 0 to 5 come out twice as likely to be returned compared to any other number!

            Any comments?

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

            @JonB
            yep, that's about right, and the cause for my tagged comment about

            it will "reduce" the randomness.


            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.

            JonBJ 1 Reply Last reply
            1
            • J.HilkJ J.Hilk

              @JonB
              yep, that's about right, and the cause for my tagged comment about

              it will "reduce" the randomness.

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

              @J.Hilk
              :) Is there a suggested algorithm somewhere for generating "truly" random numbers within a range, e.g. as per the case above, generalized for a range r? I can think of: if the random number is greater equal 250 throw it away and draw again, but obviously this does not scale well efficiency-wise....!

              J.HilkJ kshegunovK 2 Replies Last reply
              0
              • JonBJ JonB

                @J.Hilk
                :) Is there a suggested algorithm somewhere for generating "truly" random numbers within a range, e.g. as per the case above, generalized for a range r? I can think of: if the random number is greater equal 250 throw it away and draw again, but obviously this does not scale well efficiency-wise....!

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

                @JonB said in Normalize random() output?:

                @J.Hilk
                :) Is there a suggested algorithm somewhere for generating "truly" random numbers within a range, e.g. as per the case above, generalized for a range r? I can think of: if the random number is greater equal 250 throw it away and draw again, but obviously this does not scale well efficiency-wise....!

                Thats a tough question.

                if your target range is 0-3, 0-7, 0-15, 0-31, 0-63, 0-127, 69-127 etc , you could do some bit shifting/setting, but after that it gets complicated. I would suggest looking into boost libary or something like that those guys usually have some nifty solutions for such problems :)


                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
                • JonBJ JonB

                  @J.Hilk
                  :) Is there a suggested algorithm somewhere for generating "truly" random numbers within a range, e.g. as per the case above, generalized for a range r? I can think of: if the random number is greater equal 250 throw it away and draw again, but obviously this does not scale well efficiency-wise....!

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #8

                  Scale and shift (for generation between 0 and generatorMax):

                  int randomNumber, generatorMax;
                  int max, min; //< Range
                  int scaledNumber = min + static_cast<double>(randomNumber) / generatorMax * (max - min);
                  

                  PS. One should probably round at the end, instead of truncating, but I forgot x_x.

                  Read and abide by the Qt Code of Conduct

                  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