Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to generate a UUID
Published On: August 20, 2009
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

As explained in Wikipedia:

"A Universally Unique Identifier (UUID) is an identifier standard used in software construction, standardized by the Open Software Foundation (OSF) as part of the Distributed Computing Environment (DCE). The intent of UUIDs is to enable distributed systems to uniquely identify information without significant central coordination. Thus, anyone can create a UUID and use it to identify something with reasonable confidence that the identifier will never be unintentionally used by anyone for anything else. Information labeled with UUIDs can therefore be later combined into a single database without needing to resolve name conflicts."

4D makes extensive use of UUIDs internally. Applications that use the iCalendar format make extensive user of UUIDs to ID calendars and events. Every hard drive in your computer has a UUID. And now you can add UUIDs to your own database elements in 4D v11 SQL.

The routine below, UTIL_NewUUID, returns a version 4 UUID as specified in rfc 4122. Note the UTIL_Random command is from the Tech Tip: A wrapper for Random with added functionality.



`====================== Initialize and Setup ================================


`======================== Method Actions ==================================

`// rfc 4122 requires this format for a version 4 (random number) UUID

For ($Ndx;1;Length($UUID_T))
    If ($UUID_T[[$Ndx]]="#")
        $Rnd_L:=UTIL_Random (0;$End_L)
        If ($Ndx=20)
            `Must be a value of 8, 9, a, or b
            $Idx:=($Rnd_L & 0x0003) | 0x0008
        End if
    End if
End for

`======================== Clean up and Exit =================================


Commented by Neil Dennis on April 29, 2010 at 1:01 PM
WARNING: Testing the generated UUIDs for uniqueness I was able to generate over 200 million without finding a single duplicate. However the UTIL_Random does not seem to produce sufficiently random numbers after a database restart. I found that after only three database restarts and generating IDs I discovered many duplicate UUIDs.