Module:RandomItemSequence: Difference between revisions
Faintfulness (talk | contribs) (Lua script for randomized flavor seasoning template) |
Faintfulness (talk | contribs) No edit summary |
||
| Line 2: | Line 2: | ||
local Random = require('Module:Random') | local Random = require('Module:Random') | ||
local | -- Fisher–Yates shuffle | ||
local function shuffle(t) | |||
for i = #t, 2, -1 do | |||
local j = Random.random(i) | |||
t[i], t[j] = t[j], t[i] | |||
end | |||
end | |||
function p. | -- render function with optional reseeding | ||
-- frame: standard MediaWiki frame | |||
-- reseed: boolean (default true) — set false for deterministic output | |||
function p.render(frame, reseed) | |||
-- reseed per invocation unless explicitly disabled | |||
if reseed ~= false then | |||
Random.seed(os.clock() * 1e9 + os.time()) | |||
end | |||
local args = frame:getParent().args | |||
local itemsParam = args.items or "" | |||
local tooltip = args.tooltip or "" | |||
local link = args.link or "" | |||
local items = {} | |||
for item in string.gmatch(itemsParam, '([^;]+)') do | |||
item = item:gsub("^%s*(.-)%s*$", "%1") | |||
table.insert(items, item) | |||
end | |||
if #items == 0 then | |||
return "" | |||
end | |||
shuffle(items) | |||
return frame:expandTemplate{ | |||
title = "ItemSlot", | |||
args = { | |||
item = table.concat(items, "; "), | |||
tooltip = tooltip, | |||
link = link | |||
} | |||
} | |||
end | end | ||
return p | return p | ||
Revision as of 02:12, 28 December 2025
Documentation for this module may be created at Module:RandomItemSequence/doc
local p = {}
local Random = require('Module:Random')
-- Fisher–Yates shuffle
local function shuffle(t)
for i = #t, 2, -1 do
local j = Random.random(i)
t[i], t[j] = t[j], t[i]
end
end
-- render function with optional reseeding
-- frame: standard MediaWiki frame
-- reseed: boolean (default true) — set false for deterministic output
function p.render(frame, reseed)
-- reseed per invocation unless explicitly disabled
if reseed ~= false then
Random.seed(os.clock() * 1e9 + os.time())
end
local args = frame:getParent().args
local itemsParam = args.items or ""
local tooltip = args.tooltip or ""
local link = args.link or ""
local items = {}
for item in string.gmatch(itemsParam, '([^;]+)') do
item = item:gsub("^%s*(.-)%s*$", "%1")
table.insert(items, item)
end
if #items == 0 then
return ""
end
shuffle(items)
return frame:expandTemplate{
title = "ItemSlot",
args = {
item = table.concat(items, "; "),
tooltip = tooltip,
link = link
}
}
end
return p