Skip to content

Instantly share code, notes, and snippets.

@jrus
Created July 29, 2012 09:26
Show Gist options
  • Save jrus/3197011 to your computer and use it in GitHub Desktop.
Save jrus/3197011 to your computer and use it in GitHub Desktop.
quick lua implementation of "random" UUID
local random = math.random
local function uuid()
local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
return string.gsub(template, '[xy]', function (c)
local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
return string.format('%x', v)
end)
end
@denisdemais
Copy link

fine this works!

@shmoli-itrs
Copy link

Need to seed math.random to avoid generating the same uuid in subsequent runs

@minipunch
Copy link

minipunch commented Aug 5, 2018

Thanks, good enough for my needs I think

@wandersonwhcr
Copy link

ty!

@llucere
Copy link

llucere commented Jul 1, 2021

thanks

@pricard32
Copy link

Need to seed math.random to avoid generating the same uuid in subsequent runs

Can you show me your modified version?
I’ve got same uuid when generate it multiple time

@Ethan-Pixelate
Copy link

Need to seed math.random to avoid generating the same uuid in subsequent runs

Can you show me your modified version? I’ve got same uuid when generate it multiple time

An easy solution for me is to simply do this:
math.randomseed(os.time())
The seed will be the same if the program is run within the same second, but i think that it's a very rare case. So it's likely fine

@wtyqwer
Copy link

wtyqwer commented Jul 7, 2022

your can use this seed to avoid same uuid
math.randomseed(tonumber(tostring(os.time()):reverse():sub(1, 9)))

@lost-scripts
Copy link

Or I guess one could simply use os.clock() instead os.time()? Or maybe a mixture of both to ensure different UUIDs no matter what, I think I'll try that and see... Thanks for the simple code, BTW!

@lost-scripts
Copy link

lost-scripts commented Dec 9, 2022

Hmm... any reason it always throws a 31 ending UUID? I've tried several "math.randomseed" possibilities I could think (included the suggested one a couple of comments above) and, even though everything else seems right, the always repeating 31 figure at the end kind of intrigues me... Does anyone else have encountered this behavior or has any idea of how could it be solved?

@lost-scripts
Copy link

Ahhh... I think I got it... the 31 figure at the end of the resulting UUIDs, doesn't seem to be anything else than the number of matches returned by string.gsub that, for some reason, the program from I run Lua embedded is joining to the very end of the first returned parameter (the UUID itself) instead of showing it separated with a tab, space or whatever. So now I only have to find the way to get rid of such parameter or avoid it is captured/returned, ideally without complicating things too much...

@lost-scripts
Copy link

And there it is! FTR, and as stated here, as soon as I wrapped all the string.gsub part into parenthesis, only the UUID is returned by the main function an everything seems to work as expected, cool! Thankfully, in Lua the simple addition of some parenthesis here or there is usually enough to save the day :)

@SomewhatMay
Copy link

And there it is! FTR, and as stated here, as soon as I wrapped all the string.gsub part into parenthesis, only the UUID is returned by the main function an everything seems to work as expected, cool! Thankfully, in Lua the simple addition of some parenthesis here or there is usually enough to save the day :)

You can try this:

return {string.format('%x', v)}[1]

You just wrap it into a table and then index the first object in that table.

@jlaurens
Copy link

Alternate solution without an auxiliary table: replace in the top function

return string.gsub(...)

with

local ans = string.gsub(...)
return ans

The first return value of string.gsub is assigned to ans, the others are ignored

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment