Posted: 22nd Nov 2023 18:16
Hello everyone, made this snippet for a multiplayer server system I was developing. It's pretty straight forward, generates a new UUID String according to the v4 spec

+ Code Snippet
function CreateUUID()
	nibbles as integer[31]
	
	//Generates 128 random bits
	for i=0 to 31
		bit0 = Random(0,1)
		bit1 = Random(0,1)
		bit2 = Random(0,1)
		bit3 = Random(0,1)
		nibbles[i] = (bit3<<3)+(bit2<<2)+(bit1<<1)+bit0
	next i
	
	//Set the version number
	nibbles[12]=nibbles[12]&&0x0
	nibbles[12]=nibbles[12]||0x4
	
	//Set the variant number
	nibbles[16]=nibbles[16]&&0x3
	nibbles[16]=nibbles[16]||0x8
	
	nibbles[17]=nibbles[17]&&0xF
	nibbles[17]=nibbles[17]||0x0
	
	//Convert to Hex string
	UUID$ = ""
	
	for i=0 to 31
		UUID$ = UUID$+hex(nibbles[i])
		
		if(i=7 or i=11 or i=15 or i=19)
			UUID$ = UUID$+"-"
		endif
	next i
	
endfunction UUID$
Posted: 22nd Nov 2023 18:31
UUID's are a great tool if you need a way to uniquely identify a resource. Let's say for example you have 100 different player defined types in a list, you can give them each a uuid to tell them apart and reference a specific instance. This approach relies entirely on pseudo random number generation and each individual bit is generated with a Random(0,1) call then converted to hex. Notice as well that we assign specific values in the same position everytime. This is because uuid's actually reserve a few bits to indicate what type of uuid it is.

Wanted to post my source for how this was generated: https://digitalbunker.dev/understanding-how-uuids-are-generated/
Posted: 2nd Jan 2024 20:01
I think I asked you this on discord back in november, but what's the likely hood of generated a duplicate if this were run from multiple systems? Like if I wanted each client to generate an ID and submit that to the server.