Can I use os.tmpname for random.seed on html5

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

Use socket.gettime() instead. It returns time in seconds, with millisecond precision.

Another option for generating an UUID is this Lua module: https://github.com/Tieske/uuid

It has multiple options, one being the use of mac address (which you can get from iterating sys.get_ifaddrs()).

5 Likes

You could collect mouse position movement data and combine it with socket.gettime()

The original Super Mario Bros. for NES did its seed based on the time between starting the cart and pressing start.

4 Likes