Module:Random: Difference between revisions
From MuttWiki
Jump to navigationJump to search
No edit summary |
No edit summary |
||
(3 intermediate revisions by the same user not shown) | |||
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 | |||
local function fixArgs(argsIn) | |||
local out = {} | |||
for k,v in pairs(argsIn) do | |||
out[k] = v | |||
end | |||
return out | |||
end | end | ||
function p.num(frame) | function p.num(frame) | ||
setseed() | setseed(frame.args.mode) | ||
if # | local args = fixArgs(frame.args) | ||
if #args == 0 then | |||
return math.random() | return math.random() | ||
elseif # | elseif #args == 1 then | ||
return math.random(tonumber(frame.args[1])) | return math.random(tonumber(frame.args[1])) | ||
elseif # | elseif #args >= 2 then | ||
local _min = tonumber(frame.args[1]) | local _min = tonumber(frame.args[1]) | ||
local _max = tonumber(frame.args[2]) | local _max = tonumber(frame.args[2]) | ||
Line 21: | Line 37: | ||
function p.pick(frame) | function p.pick(frame) | ||
setseed() | 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