Last edited one month ago
by Max Mustermann
No categories assignedEdit
Revision as of 11:14, 20 March 2026 by Max Mustermann (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:UUID/doc

local p = {}

-- Generate a random hex string of length n
local function randhex(n)
    local s = ""
    for i = 1, n do
        s = s .. string.format("%x", math.random(0, 15))
    end
    return s
end

function p.uuid()
    -- Seed using time and a random factor
    math.randomseed(os.time() + math.random(1, 1000000))

    -- UUID v4 format: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
    local uuid = string.format(
        "%s-%s-4%s-%s%s-%s",
        randhex(8),
        randhex(4),
        randhex(3),
        string.format("%x", math.random(8, 11)),
        randhex(2),
        randhex(12)
    )

    return uuid
end

return p