Jump to content

Module:Choose random TAFI

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Esquivalience (talk | contribs) at 04:04, 1 August 2015 (better prng?). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}
require "bit32"

-- Named argument |list= used, like: {{#invoke:Sandbox/Evad37/Random TAFI|choose|list=#[[item1]] #[[item2]] #[[item3]]}}
function p.choose(frame)
	if mw.isSubsting() then
		-- 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 number = mw.site.stats.edits + mw.site.stats.pages + os.time() + math.floor(os.clock() * 1000000000)
		
		number = bit.bxor(number, bit.arshift(number, 12))
		number = bit.bxor(number, bit.lshift(number, 25))
		number = bit.bxor(number, bit.arshift(number, 27))

		number = (number * 2685821657736338717) % 18446744073709551616
		
		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[number % (#articles + 1 - 2) + 2] -- Note: First substring is empty as list begins with a #
		return mw.text.trim(chosen)                        -- Trim whitespace before returning
	else return "Error: Must be substituted – use {{subst:#invoke: ... }}"
	end
end

return p