Module:Random

From MuttWiki
Revision as of 04:29, 22 February 2025 by Bofh (talk | contribs)
Jump to navigationJump to search

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

local p = {}

local function setseed()
	math.randomseed(math.floor(os.time() / 3600))
end

local function fixArray(tab)
	local out = {}
	for k,v in pairs(tab) do
		table.insert(out, k, v)
	end
	return out
end

function p.num(frame)
	setseed()
	local args = fixArray(frame.args)
	if #args == 0 then
		return math.random()
	elseif #args == 1 then
		return math.random(tonumber(frame.args[1]))
	elseif #args >= 2 then
		local _min = tonumber(frame.args[1])
		local _max = tonumber(frame.args[2])
	end
	if _min > _max then
		_min, _max = _max, _min
	end
end

function p.pick(frame)
	setseed()
	local args = fixArray(frame.args)
	return args[math.random(#args)]
end

return p