Module:Random choice
Appearance
Returns a random pipe-separated segment of the argument (for {{Random choice}}).
Is made to be above all fast, the probability of an argument being chosen is proportional to its length.
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