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. Generating Unique random numbers QRandomGenerator
QtWS25 Last Chance

Generating Unique random numbers QRandomGenerator

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 2.0k 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.
  • H Offline
    H Offline
    Havaloow
    wrote on last edited by
    #1

    Hello dears,

    Hope you all are doing well.

    I have a question about QRandomGenerator Seeding for creating unique numbers every time i call (Generate).

    if i seed the generator with different numbers every time i call the function , does it guarantee that the generated number will be unique and not repeated with the previews calls?

    for example if i seed the generator with seconds elapsed since 1990 , the seed will be different everytime!.

    Thanks for your help and suggestions.

    J.HilkJ 1 Reply Last reply
    0
    • H Havaloow

      Hello dears,

      Hope you all are doing well.

      I have a question about QRandomGenerator Seeding for creating unique numbers every time i call (Generate).

      if i seed the generator with different numbers every time i call the function , does it guarantee that the generated number will be unique and not repeated with the previews calls?

      for example if i seed the generator with seconds elapsed since 1990 , the seed will be different everytime!.

      Thanks for your help and suggestions.

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

      @Havaloow
      taken from https://doc.qt.io/qt-5/qrandomgenerator.html#seeding-and-determinism

      Due to mixing of the seed data, QRandomGenerator cannot guarantee that distinct seeds will produce different sequences.

      so, no , there is no guarantee,

      How ever the next sentence

      QRandomGenerator::global(), like all generators created by QRandomGenerator::securelySeeded(), is always seeded from QRandomGenerator::system(), so it's not possible to make it produce identical sequences.

      Seems like if you use that, you should be fine :D


      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.

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

        @Havaloow
        taken from https://doc.qt.io/qt-5/qrandomgenerator.html#seeding-and-determinism

        Due to mixing of the seed data, QRandomGenerator cannot guarantee that distinct seeds will produce different sequences.

        so, no , there is no guarantee,

        How ever the next sentence

        QRandomGenerator::global(), like all generators created by QRandomGenerator::securelySeeded(), is always seeded from QRandomGenerator::system(), so it's not possible to make it produce identical sequences.

        Seems like if you use that, you should be fine :D

        H Offline
        H Offline
        Havaloow
        wrote on last edited by
        #3

        @J-Hilk Thanks for your replay.

        To be more clear i store those numbers as an ID's of customers in local database then upload them to the master database , so in my case i have several local databases and one master database.

        I need these numbers to be unique on all different machines.

        Does that QRandomGenerator::global(), produce different unique numbers everywhere?

        Thanks again

        J.HilkJ JonBJ 2 Replies Last reply
        0
        • H Offline
          H Offline
          Havaloow
          wrote on last edited by
          #4

          To make things more clear those IDs are binded to other transactions on database so if i upload it to master database if even one ID's are identical conflict will happen and all data's will be corrupt.

          I cannot use auto_Increment for IDs because local machines increment ID's according to there own sequence!.

          Thanks for any suggestions and help.

          1 Reply Last reply
          0
          • H Havaloow

            @J-Hilk Thanks for your replay.

            To be more clear i store those numbers as an ID's of customers in local database then upload them to the master database , so in my case i have several local databases and one master database.

            I need these numbers to be unique on all different machines.

            Does that QRandomGenerator::global(), produce different unique numbers everywhere?

            Thanks again

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

            @Havaloow well
            I'm not so into QRandomGenerator, so I can't say for sure. But I would say, that there is a chance (probably very small one) that you get doublicates.

            There's as an alternative QUuid

            But that is also only guaranteed for the system the code is called upon.

            The correct way to do this, would be to connect first, with an known fixed ID to your masterdatabase, and than the master returns a new unique ID the the customer (checking previously if generated before)


            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.

            H 1 Reply Last reply
            0
            • H Havaloow

              @J-Hilk Thanks for your replay.

              To be more clear i store those numbers as an ID's of customers in local database then upload them to the master database , so in my case i have several local databases and one master database.

              I need these numbers to be unique on all different machines.

              Does that QRandomGenerator::global(), produce different unique numbers everywhere?

              Thanks again

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

              @Havaloow said in Generating Unique random numbers QRandomGenerator:

              Does that QRandomGenerator::global(), produce different unique numbers everywhere?

              No. If it did they would not be "random".

              I need these numbers to be unique on all different machines.

              I believe UUIDs, like @J-Hilk's suggestion, do include a segment which is based on individual machine (mac address?), but you should check.

              Your database may have a feature for generating unique numbers, not auto-incrementing. Uniqueness may be required for replication. Again you would have to read up on your database's documentation.

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

                @Havaloow well
                I'm not so into QRandomGenerator, so I can't say for sure. But I would say, that there is a chance (probably very small one) that you get doublicates.

                There's as an alternative QUuid

                But that is also only guaranteed for the system the code is called upon.

                The correct way to do this, would be to connect first, with an known fixed ID to your masterdatabase, and than the master returns a new unique ID the the customer (checking previously if generated before)

                H Offline
                H Offline
                Havaloow
                wrote on last edited by
                #7

                @J-Hilk
                That would be correct for an (All time connected master and slave) , the problem in my environment is that i can only connect and sync at mid-night .

                1 Reply Last reply
                0
                • gde23G Offline
                  gde23G Offline
                  gde23
                  wrote on last edited by
                  #8

                  The numbers - also those coming from some secure random like /dev/random are not granted to be unique. What is granted there is that you will (hopefully) never get the same sequence of random numbers, because that would break many crypto algorithms.

                  However still it is very very unlikely that you get the same number twice. This is also how hash tables for databases work. There is a small chance of a collision (two datasets getting the same hash value) but the chance is really small and therefore it is an accepted risk. However if you still want really unique numbers i would just generate a number, then look in the database if it already exists and if it exists try again.

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Havaloow said in Generating Unique random numbers QRandomGenerator:

                    Does that QRandomGenerator::global(), produce different unique numbers everywhere?

                    No. If it did they would not be "random".

                    I need these numbers to be unique on all different machines.

                    I believe UUIDs, like @J-Hilk's suggestion, do include a segment which is based on individual machine (mac address?), but you should check.

                    Your database may have a feature for generating unique numbers, not auto-incrementing. Uniqueness may be required for replication. Again you would have to read up on your database's documentation.

                    H Offline
                    H Offline
                    Havaloow
                    wrote on last edited by
                    #9

                    @JonB Thanks.

                    if Qt UUID has a segment related to mac address then it has to be unique on every machine? am i right ?

                    I've kind of hoped that i find better solution than MySQL UUID solution because its too lengthy to use for ID's. however if i dont find any better solutions i think i have to use MySQL Uuid.

                    Thanks guys for your answers.

                    JonBJ 1 Reply Last reply
                    0
                    • H Havaloow

                      @JonB Thanks.

                      if Qt UUID has a segment related to mac address then it has to be unique on every machine? am i right ?

                      I've kind of hoped that i find better solution than MySQL UUID solution because its too lengthy to use for ID's. however if i dont find any better solutions i think i have to use MySQL Uuid.

                      Thanks guys for your answers.

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by JonB
                      #10

                      @Havaloow said in Generating Unique random numbers QRandomGenerator:

                      I've kind of hoped that i find better solution than MySQL UUID solution because its too lengthy to use for ID's.

                      Why? It's 16 bytes long. You can use id BINARY(16) PRIMARY KEY. You could read https://www.mysqltutorial.org/mysql-uuid/, and compare against https://www.percona.com/blog/2019/11/22/uuids-are-popular-but-bad-for-performance-lets-discuss/. But my guess: unless you are storing millions of these in large data, I highly doubt you'll notice any performance issues.....

                      1 Reply Last reply
                      1
                      • H Offline
                        H Offline
                        Havaloow
                        wrote on last edited by
                        #11

                        Thank you guys for your suggestions and answers.

                        It appears that the only solution is MySQL UUID for my case.

                        However ive found another unique ID generation with date and time and a specific unique machine related prifix with this format.
                        Pfx-Date-Time
                        (SA-202008181007334)

                        i keep this post unsolved for anyone who likes to add their solutions.

                        Thanks.

                        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