The problem is that os.time() give value just in secons. and os.clock() start from 0.
If Several players start game at the same time and so it’s a big chance that they will get the same uuid because of the same random seed. I found that os.tmpname generate some temp file, but i don’t know the algorithm.
So the question is it ok to use something like this. (first random seed based on tmpname)
for random function on uuid generation
local start_seed = tonumber(hash_to_hex(hash(os.tmpname())), 16)
math.randomseed( start_seed)
math.random()
math.random()
...
local function rnd(from, to)
local seed = os.time() + (os.clock()*1000000) + math.random(0, 65535)
math.randomseed(seed)
math.random()
math.random()
return math.random(from, to)
end
...
local function getUUID()
local _rnd = rnd
local _fmt = string.format
--
--_rnd()
--
local time_low_a = _rnd(0, 65535)
local time_low_b = _rnd(0, 65535)
--
local time_mid = _rnd(0, 65535)
--
local time_hi = _rnd(0, 4095 )
time_hi = padbits( num2bs(time_hi), 12 )
local time_hi_and_version = bs2num( "0100" .. time_hi )
--
local clock_seq_hi_res = _rnd(0,63)
clock_seq_hi_res = padbits( num2bs(clock_seq_hi_res), 6 )
clock_seq_hi_res = "10" .. clock_seq_hi_res
--
local clock_seq_low = _rnd(0,255)
clock_seq_low = padbits( num2bs(clock_seq_low), 8 )
--
local clock_seq = bs2num(clock_seq_hi_res .. clock_seq_low)
--
local node = {}
for i=1,6 do
node[i] = _rnd(0,255)
end
--
local guid = ""
guid = guid .. padbits(_fmt("%X",time_low_a), 4)
guid = guid .. padbits(_fmt("%X",time_low_b), 4) .. "-"
guid = guid .. padbits(_fmt("%X",time_mid), 4) .. "-"
guid = guid .. padbits(_fmt("%X",time_hi_and_version), 4) .. "-"
guid = guid .. padbits(_fmt("%X",clock_seq), 4) .. "-"
--
for i=1,6 do
guid = guid .. padbits(_fmt("%X",node[i]), 2)
end
--
return guid
end