# UID

## Recommended Posts

Thanks to datkewlguy and all the people that worked with him to create the Scramble functions.

These functions can produce and check a unique number that can be used to identify things. It uses a system of random numbers and thier sum converted into hex and scrambled to create a UID.

Here's the code:

```Func _UIDCreate()
Local \$n_1 = Int(Random(1, 255)), \$n_2 = Int(Random(1, 255)), \$n_3 = Int(Random(0, 255)), \$n_4 = Int(Random(0, 255)), \$n_5 = Hex(\$n_1 + \$n_2 + \$n_3 + \$n_4, 8), \$n_1 = Hex(\$n_1, 2), \$n_2 = Hex(\$n_2, 2), \$n_3 = Hex(\$n_3, 2), \$n_4 = Hex(\$n_4, 2)
Return _Scramble(\$n_1 & \$n_2 & \$n_3 & \$n_4 & "-" & \$n_5)
EndFunc

Func _UIDCheck(\$sz_UID)
If \$sz_UID = "0000000000000000-" Then Return 0
Local \$sz_decUID = _UnScramble(\$sz_UID), \$sz_tsplit = StringSplit(\$sz_decUID, "-"), \$sz_part1 = StringSplit(\$sz_tsplit, ""), \$n_dec1 = Dec(\$sz_part1 & \$sz_part1), \$n_dec2 = Dec(\$sz_part1 & \$sz_part1), \$n_dec3 = Dec(\$sz_part1 & \$sz_part1), \$n_dec4 = Dec(\$sz_part1 & \$sz_part1)
If \$n_dec1 + \$n_dec2 + \$n_dec3 + \$n_dec4 = Dec(\$sz_tsplit) Then Return 1
Return 0
EndFunc```

I don't really know what this can be used for I just thought it would be a cool idea, but I do hope it can be of help to someone. And, as always , questions/comments are greatly appreciated! ##### Share on other sites

Here's a ready made one from Microsoft. Latest beta needed for DllStruct support of course.

```Opt("MustDeclareVars", 1)

ConsoleWrite(_CreateGUID() & @LF)

Func _CreateGUID()
Local \$GUIDSTRUCT
Local \$sGUID

\$GUIDSTRUCT = DllStructCreate("int;int;int;byte")
If Not @error Then
\$aDllRet = DllCall("ole32.dll", "long", "CoCreateGuid", "ptr", DllStructGetPtr(\$GUIDSTRUCT))
If Not @error And \$aDllRet = 0 Then
\$aDllRet = DllCall("ole32.dll", "long", "StringFromGUID2", _
"ptr", DllStructGetPtr(\$GUIDSTRUCT), _
"wstr", "", _
"int", 40)
EndIf
DllStructDelete(\$GUIDSTRUCT)
EndIf
Return \$sGUID
EndFunc```
##### Share on other sites

Does that Dll one produce a unique ID? If not, then this function I just whipped up is even faster. ```Func _MyGUID()
Local \$a_GUID
For \$i = 0 To 7
\$a_GUID[\$i] = Hex(Random(0, 0xFFFF), 4)
Next
Return '{' & \$a_GUID & \$a_GUID & '-' &_
\$a_GUID & '-' & \$a_GUID & '-' &_
\$a_GUID & '-' & \$a_GUID &_
\$a_GUID & \$a_GUID & '}'
EndFunc```

This one runs in about .60 seconds, the MS one seems to average up around .68. Plus mine is less code ##### Share on other sites

Does that Dll one produce a unique ID?

Yah, that's the trick. Sez Microsoft:

A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated.

BlueBearrOddly enough, this is what I do for fun.
##### Share on other sites

Does that Dll one produce a unique ID? If not, then this function I just whipped up is even faster. ```Func _MyGUID()
Local \$a_GUID
For \$i = 0 To 7
\$a_GUID[\$i] = Hex(Random(0, 0xFFFF), 4)
Next
Return '{' & \$a_GUID & \$a_GUID & '-' &_
\$a_GUID & '-' & \$a_GUID & '-' &_
\$a_GUID & '-' & \$a_GUID &_
\$a_GUID & \$a_GUID & '}'
EndFunc```

This one runs in about .60 seconds, the MS one seems to average up around .68. Plus mine is less code <{POST_SNAPBACK}>

Not sure what algo MS uses to generate GUIDs but I believe it's slightly more sophisticated than your random method.

Have a look here for a bit more info on the actual function that generates the GUIDs.

To quote from the Remarks section:

Computers with ethernet/token ring addresses generate UUIDs that are guaranteed to be globally unique

##### Share on other sites

Does that Dll one produce a unique ID? If not, then this function I just whipped up is even faster. ```Func _MyGUID()
Local \$a_GUID
For \$i = 0 To 7
\$a_GUID[\$i] = Hex(Random(0, 0xFFFF), 4)
Next
Return '{' & \$a_GUID & \$a_GUID & '-' &_
\$a_GUID & '-' & \$a_GUID & '-' &_
\$a_GUID & '-' & \$a_GUID &_
\$a_GUID & \$a_GUID & '}'
EndFunc```

This one runs in about .60 seconds, the MS one seems to average up around .68. Plus mine is less code <{POST_SNAPBACK}>

Yes, but how would you check it to make sure it is valid? Mine is able to be checked because it just adds up the decimal values and converts to hex.

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...