Module:Randomize list

From Meta, a Wikimedia project coordination wiki
Module documentation

This module randomizes the order of a list; it accepts an unlimited amount of parameters (the list contents). Randomization will occur every time the page is re-parsed; purging can trigger this process.

Example usage[edit]

Code[edit]

{{#invoke:Randomize list|main|apple|banana|cherry}}

Output[edit]

cherrybananaapple

TemplateData[edit]

Randomizes the order of a list, accepting an unlimited amount of unnamed parameters (the list contents).

Template parameters

ParameterDescriptionTypeStatus
Item 11

First item of list

Example
apple
Unknownrequired
Item 22

Second item of list

Example
banana
Unknownrequired
Item 33

Third item of list

Example
cherry
Unknownoptional

-- Randomizes the order of input parameters
-- By [[User:EpicPupper]]

local p = {}

function p.main(frame)
    -- Get all the input strings from the template parameters
    local inputs = {}
    for i, v in ipairs(frame.args) do
        table.insert(inputs, v)
    end
    
    -- Randomly shuffle the input strings
    math.randomseed(os.time())
    for i = #inputs, 2, -1 do
        local j = math.random(i)
        inputs[i], inputs[j] = inputs[j], inputs[i]
    end
    
    -- Generate the output string
    local output = table.concat(inputs)
    
    -- Return the output string
    return output
end

return p