Module:Random: Difference between revisions

From MuttWiki
Jump to navigationJump to search
testing
 
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


function p.test(frame)
local function setseed(mode)
return "This is a test!"
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
end


return p
return p

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