Module:Random: Difference between revisions
From MuttWiki
				
				
				Jump to navigationJump to search
				
				
No edit summary  | 
				No edit summary  | 
				||
| Line 1: | Line 1: | ||
local p = {}  | local p = {}  | ||
local function setseed()  | local function setseed(mode)  | ||
	math.randomseed(math.floor(os.time() / 3600))  | 	if mode == 'hour' then  | ||
		math.randomseed(math.floor(os.time()/3600))  | |||
	elseif mode == 'stat' then  | |||
		local stats = mw.site.stats  | |||
		math.randomseed(stats.pages*23 + stats.edits*19)  | |||
	else  | |||
		math.randomseed(os.time() + math.floor(os.clock()*1000000000))  | |||
	end  | |||
end  | end  | ||
| Line 8: | Line 15: | ||
	local out = {}  | 	local out = {}  | ||
	for k,v in pairs(argsIn) do  | 	for k,v in pairs(argsIn) do  | ||
		out[k] = v  | |||
	end  | 	end  | ||
	return out  | 	return out  | ||
| Line 18: | Line 21: | ||
function p.num(frame)  | function p.num(frame)  | ||
	setseed()  | 	setseed(frame.args.mode)  | ||
	local args = fixArgs(frame.args)  | 	local args = fixArgs(frame.args)  | ||
	if #args == 0 then  | 	if #args == 0 then  | ||
| Line 34: | Line 37: | ||
function p.pick(frame)  | function p.pick(frame)  | ||
	setseed()  | 	setseed(frame.args.mode)  | ||
	local args = fixArgs(frame.args)  | 	local args = fixArgs(frame.args)  | ||
	return args[math.random(#args)]  | 	return args[math.random(#args)]  | ||
Latest revision as of 04:53, 22 February 2025
Documentation for this module may be created at Module:Random/doc
local p = {}
local function setseed(mode)
	if mode == 'hour' then
		math.randomseed(math.floor(os.time()/3600))
	elseif mode == 'stat' then
		local stats = mw.site.stats
		math.randomseed(stats.pages*23 + stats.edits*19)
	else
		math.randomseed(os.time() + math.floor(os.clock()*1000000000))
	end
end
local function fixArgs(argsIn)
	local out = {}
	for k,v in pairs(argsIn) do
		out[k] = v
	end
	return out
end
function p.num(frame)
	setseed(frame.args.mode)
	local args = fixArgs(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(frame.args.mode)
	local args = fixArgs(frame.args)
	return args[math.random(#args)]
end
return p