Content deleted Content added
module containing various functions that use random numbers |
add a randomizeToText function and factor out shared code with the randomize function |
||
Line 41:
return sep
end
local function randomizeArray(t)
-- Randomizes an array. It works by iterating through the list backwards, each time swapping the entry
--
local r = math.random(i)▼
end
return t
end
local function makeRandomList(args)
local list = removeBlanks(args)▼
list = randomizeArray(list)
return list
end
Line 65 ⟶ 81:
function p._randomize(args)
-- Randomizes a list and concatenates the result with a separator.
local list = makeRandomList(args)
▲ -- Randomization algorithm courtesy of Xinhuan at http://forums.wowace.com/showthread.php?p=279756
▲ local list = removeBlanks(args)
▲ for i = #list, 2, -1 do
▲ local r = math.random(i)
▲ list[i], list[r] = list[r], list[i]
▲ end
local sep = makeSeparator(args.sep or args.separator)
return table.concat(list, sep)
end
function p._randomizeToText(args)
-- Randomizes a list and concatenates the result, text-style. Accepts separator and conjugation arguments.
local list = makeRandomList(args)
local sep = makeSeparator(args.sep or args.separator)
local conj = makeSeparator(args.conj or args.conjugation)
return mw.text.listToText(list, sep, conj)
end
|