Share your advanced PureBasic knowledge/code with the community.
Mistrel
Addict
Posts: 3415 Joined: Sat Jun 30, 2007 8:04 pm
Post
by Mistrel » Wed Jul 01, 2009 5:44 am
According to Wikipedia this conforms to version 4 (random) OSF UUID standard:
Code: Select all
#UUID_FormatType_None=1<<(0)
#UUID_FormatType_Dashes=1<<(1)
#UUID_FormatType_Braces=1<<(2)
Structure UUID
Byte.b[16]
EndStructure
Procedure.i UniqueID(*UUID.UUID)
If Not *UUID
ProcedureReturn 0
EndIf
For i=0 To 16-1
*UUID\Byte[i]=Random(255)
Next
*UUID\Byte[9]=128+Random(63)
*UUID\Byte[7]=64+Random(15)
ProcedureReturn *UUID
EndProcedure
Procedure.s FormatUUID(*UUID.UUID, FormatType=#UUID_FormatType_None)
Protected UUID_String.s
For i=0 To 16-1
If FormatType&#UUID_FormatType_Dashes
If i=3 Or i=5 Or i=7
UUID_String.s+"-"
EndIf
EndIf
UUID_String.s+RSet(Hex(*UUID\Byte[i]&$FF),2,"0")
Next
If FormatType&#UUID_FormatType_Braces
UUID_String.s="{"+UUID_String.s+"}"
EndIf
ProcedureReturn UUID_String.s
EndProcedure
Define UUID.UUID
Debug FormatUUID(UniqueID(@UUID),#UUID_FormatType_Dashes|#UUID_FormatType_Braces)
Mistrel
Addict
Posts: 3415 Joined: Sat Jun 30, 2007 8:04 pm
Post
by Mistrel » Wed Jul 01, 2009 6:07 am
Here is a smaller version without the version bytes:
Code: Select all
For i=0 To 16-1
GUID.s+RSet(Hex(Random(255)&$FF),2,"0")
Next
Debug GUID.s
Mistrel
Addict
Posts: 3415 Joined: Sat Jun 30, 2007 8:04 pm
Post
by Mistrel » Wed Jul 01, 2009 9:07 pm
Another method if you want more granular control:
Code: Select all
For i=0 To 32-1
GUID.s+Chr(Asc(Hex(Random(15))))
Next
Debug GUID.s
StarBootics
Addict
Posts: 984 Joined: Sun Jul 07, 2013 11:35 am
Location: Canada
Post
by StarBootics » Wed Jan 23, 2019 3:39 am
Hello everyone,
Sorry to re-open an old thread. This is my version of Mistrel's original code. I needed this for my 3D game project.
Code: Select all
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Cross-platform UUID generator
; File Name : UUID - Module.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 22-01-2019
; Last Update : 22-01-2019
; PureBasic code : V5.70 LTS
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programming notes
;
; Based on Mistrel's original code.
; See here : https://www.purebasic.fr/english/viewtopic.php?f=12&t=38011
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
DeclareModule UUID
Declare.s Make(Wrap_With_Brackets.b = #False)
EndDeclareModule
Module UUID
Procedure.s Make(Wrap_With_Brackets.b = #False)
For Index = 0 To 15
If Index = 7
Byte.a = 64 + Random(15)
ElseIf Index = 9
Byte = 128 + Random(63)
Else
Byte = Random(255)
EndIf
If Index = 4 Or Index = 6 Or Index = 8 Or Index = 10
UUID_String.s + "-"
EndIf
UUID_String + RSet(Hex(Byte, #PB_Ascii), 2, "0")
Next
If Wrap_With_Brackets = #True
UUID_String = "{" + UUID_String + "}"
EndIf
ProcedureReturn UUID_String
EndProcedure
EndModule
CompilerIf #PB_Compiler_IsMainFile
Macro AddElementEx(ListName, Element)
AddElement(ListName)
ListName = Element
EndMacro
NewList UUID.s()
For index = 0 To 499
AddElementEx(UUID(), UUID::Make())
Next
SortList(UUID(), #PB_Sort_Ascending)
ForEach UUID()
Debug UUID()
Next
CompilerEndIf
; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
Kwai chang caine
Always Here
Posts: 5357 Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France
Post
by Kwai chang caine » Wed Jan 23, 2019 1:13 pm
Thanks for sharing
The happiness is a road...
Not a destination
davido
Addict
Posts: 1890 Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK
Post
by davido » Wed Jan 23, 2019 3:06 pm
@
StarBootics ,
I hadn't seen this, so thank you for reincarnating it.
Thank you for sharing.
DE AA EB