How is that possible?
Large numbers (2 to 128) have nearly infinite capacity.
There is no guarantee that the GUID will always be unique. There are even versions that guarantee, but you can use the version that does not guarantee. Even though it does not guarantee, possibility of collision is very small, negligible.
Is there any mathematical formula behind it, how it works?
According to Wikipedia the UUID format (the same as GUID used by Microsoft) is:
123e4567-e89b-12d3-a456-426655440000
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
4-byte (8 digits in hexadecimal representation) least significant part of UUID generation time.
2-byte (4 digits in hexadecimal representation) significant average time share of UUID generation.
2-byte (4-digit hexadecimal representation) UUID version followed by most significant part of UUID generation time.
2-byte separated into 2 fields (total 4 digits in hexadecimal representation) resolution sequence of the clock, high in the first field and low in the second, with multiplexing in the first bits to complement the version.
6-bytes (12 digits in hexadecimal representation) node adopted.
Note that this is a visual representation for humans. You do not need to store or transmit these 36 characters. Just generate a number of 128 bits, so on the computer it only needs to occupy 16 bytes.
Versions
These data are filled in different ways depending on the version. The UUID implementation of each library can do as it suits, including varying according to usage. Do not need to fill in exactly as described above, just need to follow the generation standard according to the chosen version specification.
One of the most used versions is 1, where the fill in is the time and the MAC address the unity of that machine is guaranteed.
There are some ways to ensure that two equal times are not generated if the machine’s clock does not have much resolution. The time has to be unique on the machine. And total uniqueness is to identify in which machine it was generated, assuming that clone MAC address is not done, since this is not a normal operation. I will omit the details.
Version 2 is not usually used because it involves undisclosed security protocol.
Versions 3 and 5 use a hashing of a namespace. Uniqueness is obtained based on a name that is already unique. This name can be a URL (when the inexperienced programmer with this sees a URL in a file thinks it is accessing that address, but it is only a namespace single) or one object identifier which has a logic of creating uniqueness. This form is advantageous because it can be repeatable, ie the same name generates the same UUID.
Version 3 uses MD5 and version 5 uses SHA1, much better but "heavier".
Example.
Version 4 uses a randomly generated number. This form can be used on any machine, even in the absence of a MAC address or a clock reasonable. It may have collisions, but by having a high resolution is improbable that occurs. Ideally it should be a truly random number, but a pseudorandom one is accepted.
Example.
Obviously each version has specified how these data are distributed in those fields indicated above and the algorithm will be different according to the version, but basically it is to get the required numbers according to the specification. This is usually a simple call to the operating system API and mount in the specified format.
How is this possible when we are offline?
He was created to be offline same. It doesn’t have to be networked to work. It doesn’t search for it elsewhere.
Perhaps the confusion comes because it is used as a primary key in the database, I imagine there is the idea that the client asks the server for a unique global number, but in fact the client generates on its own. It even has the advantage of be able to generate the whole record without consulting the database. Usually when we use a ID
sequential has to ask the server which is it after the insert
(example).
Generates alone because it is used in several things that only concerns the same machine.
If you pay attention to Internals much of the operating system that you access with a cute name, this name is only descriptive, your identity name is a GUID. If using the name could have name collisions and a translation of the name would make the object another, the GUID is canonical.
I wait for an answer that explains how this is possible when we are offline too.
– Marco Souza
@Marconciliosouza I believe it is a very well worked mathematical formula! Waiting for an answer here :)
– Marconi
Oxente, -1 ? Why ? was +7 now . 6 ?
– MagicHat
@Magichat I noticed here too! You will know :)
– Marconi
@Marconi I think the criticisms strengthen, but when pointed the point, so in a valley of nothing...
– MagicHat
@Magichat vdd, even to improve!
– Marconi