Page 1 of 1

(PHP like) UniqueID

Posted: Fri Nov 04, 2005 6:50 pm
by Hroudtwolf
Code updated For 5.20+

Hello,


This is the PureBasic variant of UniqueID. (But a little bit better :D )
It produce unique, unambiguous IDs for some uses.
In example... working with databases, spritelists, etc...


The function:

Code: Select all

Procedure.s CreateUniqueID(MaxLetters.l)
    If MaxLetters.l<=32 and MaxLetters.l>0
      PreID.s=Str(GetTickCount_())+Str(Random(99999))
      MD5String.s=MD5Fingerprint(@PreID.s, Len(PreID.s))
      UniqueID.s=Mid(MD5String.s,Random(32)-MaxLetters.l,MaxLetters.l)
      ProcedureReturn UniqueID.s
      Else
      ProcedureReturn "0"
   EndIf 
EndProcedure
Example:

Code: Select all

Debug CreateUniqueID(8)
Debug CreateUniqueID(10)
Debug CreateUniqueID(12)

Posted: Fri Nov 04, 2005 6:52 pm
by Trond
You don't ensure it's unique, you simply output a random ID.

Posted: Fri Nov 04, 2005 7:06 pm
by Hroudtwolf
The original php command is also based on random values.
Through the variety of possibilities, you can maintain its a unique output.

Posted: Fri Nov 04, 2005 7:57 pm
by Fred
why not simply using an incrementing counter ?

Posted: Fri Nov 04, 2005 8:25 pm
by Hroudtwolf
[edit]
There was an database error in forum
[/edit]

Posted: Fri Nov 04, 2005 8:27 pm
by Hroudtwolf
'Cause my method makes UniqueID useful for more things.
You can use it as key for encrytion, as ID-Name for files etc....
The PHP command "UniqueID" does output similar strings.
That isn't my invention.

Posted: Fri Nov 04, 2005 8:29 pm
by Rings
Fred wrote:why not simply using an incrementing counter ?
the incremental counter is for posting only ;)

Posted: Fri Nov 04, 2005 8:50 pm
by Hroudtwolf
the incremental counter is for posting only
Best example ;-)