Module:Random: Difference between revisions

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
end
 
local function randomizeArray(t)
-- Randomizes an array. It works by iterating through the list backwards, each time swapping the entry
-- Randomization"i" algorithmwith courtesya random entry. Courtesy of Xinhuan at http://forums.wowace.com/showthread.php?p=279756
for i = #listt, 2, -1 do
local r = math.random(i)
listt[i], listt[r] = listt[r], listt[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. It works by iterating through the list backwards,
local list = makeRandomList(args)
-- each time swapping the entry "i" with a random entry.
-- 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