Jump to content

Module:Choose random TAFI

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Evad37 (talk | contribs) at 02:43, 31 July 2015 (seed code from Module:Random). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

-- Named argument |list= used, like: {{#invoke:Sandbox/Evad37/Random TAFI|choose|list=#[[item1]] #[[item2]] #[[item3]]}}

function p.choose(frame)
	math.randomseed(mw.site.stats.edits + mw.site.stats.pages + os.time() + math.floor(os.clock() * 1000000000)) 
		-- Generates a different number every time the module is called, even from the same page.
		-- This is because of the variability of os.clock (the time in seconds that the Lua script has been running for).

	local list = frame.args.list                       -- List of articles inputed via a single paramtere
	local articles = mw.text.split(list, '#')          -- Split list into an array of substrings (each containing an article). 
	local chosen = articles[math.random(2, #articles)] -- Note: First substring is empty as list begins with a #
	return mw.text.trim(chosen)                        -- Trim whitespace before returning
end

return p