Jump to content

Module:Random choice

From Wikisource

require('strict')

local p = {}

local getArgs = require("Module:Arguments").getArgs

function p.main(frame)
	local args = getArgs(frame)
	local s = args[1] or ''
	math.randomseed(os.time())
	local i = math.floor(math.random()*#s)
	local j = i+1
	while (string.sub(s, i, i) ~= "|" and i > 0) do
		i = i - 1
	end
	while (string.sub(s, j, j) ~= "|" and j < #s) do
		j = j + 1
	end
	return string.sub(s, i+1, j-1)
end

return p