Creating a GUID

I wanted to know if I created a GUID correctly. GUID stands for Global Unique ID.

This is my code for an Alphabetical GUID

    // create guid and display in textarea1

var x, r as integer
var guid, guid_with_hyphens as string

for x=1 to 16
  // 65 is A
  r = 65 + System.Random.InRange(0, 25)
  guid =guid + chr(r) 
  
  select case x
  case  4 
    guid_with_hyphens=guid_with_hyphens +chr(r)+ "-"
  case  8 
    guid_with_hyphens=guid_with_hyphens +chr(r)+ "-"
  case  12 
    guid_with_hyphens=guid_with_hyphens +chr(r)+ "-"
  else
    guid_with_hyphens=guid_with_hyphens+chr(r)
  end select
  
  
next x

textarea1.Value =textarea1.Value +  guid + EndOfLine

textarea1.Value =textarea1.Value +  guid_with_hyphens + EndOfLine
textarea1.Value =textarea1.Value +  " " + EndOfLine

And this is my code for an Alphanumeric GUID

// create Alphanumeric guid and display in textarea1

var x, r as integer
var s, guid, guid_with_hyphens as string

for x=1 to 16
  
  r =  System.Random.InRange(0, 34)
  
  select case r
  case 0
    s="A"
  case 1
    s="B"
  case 2
    s="C"
  case 3
    s="D"
  case 4
    s="E"
  case 5
    s="F"
  case 6
    s="G"
  case 7
    s="H"
  case 8
    s="I"
  case 9
    s="J"
  case 10
    s="K"
  case 11
    s="L"
  case 12
    s="M"
  case 13
    s="N"
  case 14
    s="O"
  case 15
    s="P"
  case 16
    s="Q"
  case 17
    s="R"
  case 18
    s="S"
  case 19
    s="T"
  case 20
    s="U"
  case 21
    s="V"
  case 22
    s="W"
  case 23
    s="X"
  case 24
    s="Y"
  case 25
    s="Z"
  case 26
    s="1"
  case 27
    s="2"
  case 28
    s="3"
  case 29
    s="4"
  case 30
    s="5"
  case 31
    s="6"
  case 32
    s="7"
  case 33
    s="8"
  case 34
    s="9"
    // 0 is to difficult to read if it is a 0 or a O
    // case 35
    // s="0"
  end select
  
  
  guid =guid + s
  
  select case x
  case  4 
    guid_with_hyphens=guid_with_hyphens + s + "-"
  case  8 
    guid_with_hyphens=guid_with_hyphens + s + "-"
  case  12 
    guid_with_hyphens=guid_with_hyphens + s + "-"
  else
    guid_with_hyphens=guid_with_hyphens + s
  end select
  
  
next x

textarea1.Value =textarea1.Value +  guid + EndOfLine

textarea1.Value =textarea1.Value +  guid_with_hyphens + EndOfLine
textarea1.Value =textarea1.Value +  " " + EndOfLine

Is it better to store the GUID as a string without dashes? I think it is easier to read with dashes.

What should the length of the GUID be? I used 16, should it be 20 or 24?

Also, in the alphanumeric GUID, I found that 0 and O were to hard to visually identify, so I deleted creating GUID’s with 0’s in them.

Hopefully someone finds this helpful.

This had a very good discussion a few years back.

1 Like

You are creating a string of random characters. Nothing in that routines guarantees they are unique.

@Kem_Tekinay has various modules you can use free that do actual unique values, see here

2 Likes

Also an here is an easy button if you have MBS Plugins.

Function createUuid() as String
  Var u as UUIDMBS
  u=new UUIDMBS
  Var retUuidStr as String = EncodingToHexMBS(u.ValueString)
  Return retUuidStr
End Function
2 Likes

Assuming what you want is to generate an id where you won’t be generating the same one twice, I’d append something like:

Var myStr as String = DateTime.Now.SecondsSince1970.ToString

which will never repeat. You could split it up into groups of chars or whatever.

You don’t really need the date. There’s only a 1 in 340,282,366,920,938,463,463,374,607,431,768,211,455 chance of selecting the same 128-bit number twice with a decent psuedorandom number generator.

Yes, but 1 in 340,282,366,920,938,463,463,374,607,431,768,211,455 chances come up all the time. Well known fact! :rofl: