Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Generating Unique random numbers QRandomGenerator



  • 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.


  • Moderators

    @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



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



  • 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.


  • Moderators

    @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)



  • @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.



  • @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 .



  • 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.



  • @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.



  • @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.....



  • 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.


Log in to reply