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
@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