QUuid usage
-
Hi,
I need to create a unique Id in some objects I create in my project.
I was thinking of using QUuid, but I did not quite understand how to use it from the documentation.
The QUuid::createUuid() creates a totally random id, but I would like to use, for example, the version "Time".
Are there any examples?Thanks in advance.
-
Hi,
From a quick look at the documentation you would need to use the second constructor and provide the correct value for each parameter.
Out of curiosity, why do you want that specific type ?
-
I don't want to use that type in particular, I wouldn't use the random function because, as written in the documentation, doesn't generate a unique Uuid (even if the probability of duplicates are tiny).
I would also use QUuid::createUuidV5, but I don't understand how to use it.
My interest, however, is to generate unique IDs, so if there are better functions or libraries than QUuid, it's okay with me.
-
I don't want to use that type in particular, I wouldn't use the random function because, as written in the documentation, doesn't generate a unique Uuid (even if the probability of duplicates are tiny).
I would also use QUuid::createUuidV5, but I don't understand how to use it.
My interest, however, is to generate unique IDs, so if there are better functions or libraries than QUuid, it's okay with me.
@Gianluca86-0
simply useQUUid::createUuid().toString()
and be happy about your unique id ;)
On what platform are you on? -
@Gianluca86-0 said in QUuid usage:
[V4] doesn't generate a unique Uuid
Neither does V1. V2 comes closer, but still not.
The advantage of V1 is that it includes the local machine's MAC address, which effectively guarantees (assuming NIC manufacturers do their jobs right) that clashes cannot happen between machines. But V1 is much more likely to clash on a single machine than V4 is. So, for example, if you have a highly distributed system that needs many many IDs generated around the world at the same or similar time, than V1 is good (and V2 even better). But if you're generating lots of IDs on a small number of hosts, then V4 actually offers a much stronger uniqueness guarantee than V1 does.
Also, note that V1's MAC address advantage is gone if you're subject to NIC virtualisations, such as AWS and other cloud infrastructure, VirtulaBox etc (because they all use non-unique MAC addresses anyway), or if you're on a machine with no NIC at all (in which case the standards require fallback to random for those bits anyway).
So, in summary, unless you're working on a highly distributed system, then as @raven-worx said, simply use
QUUid::createUuid()
and be happy :) And if you do happen to be working on such a distributed system, then I'd recommend a specialised UUID generator just for that purpose (one with a cryptographically secure random generator).Cheers.
-
I think you are worrying about a non-problem. From https://en.wikipedia.org/wiki/Universally_unique_identifier#Random_UUID_probability_of_duplicates
the number of random version 4 UUIDs which need to be generated in order to have a 50% probability of at least one collision is 2.71 quintillion [...] This number is equivalent to generating 1 billion UUIDs per second for about 85 years
To give you prospective, the probability of winning the british national lottery are 1 in 14 millions
-
Ok, then I'll use QUUid::createUuid().
Thank you all.