Cross-platform UUID generator

Share your advanced PureBasic knowledge/code with the community.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Cross-platform UUID generator

Post by Mistrel »

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
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

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
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

Thanks!
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

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
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Cross-platform UUID generator

Post by StarBootics »

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 !
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Cross-platform UUID generator

Post by Kwai chang caine »

Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Cross-platform UUID generator

Post by davido »

@StarBootics,
I hadn't seen this, so thank you for reincarnating it.

Thank you for sharing. :D
DE AA EB
Post Reply