Module:Random

local utils = require('Module:Utils')

local random = math.random local floor = math.floor local fmod = math.fmod

local sub = string.sub local char = string.char local lower = string.lower local upper = string.upper

math.randomseed(mw.site.stats.edits + mw.site.stats.pages + os.time + math.floor(os.clock * 10^8))

local p = {}

--% This function returns a random color in hex format. --@ variant (string) controls the range of values that can be returned --: color (string) random color function p._random_color(variant) local ceil = variant and variant:match("%s*#?(%w%w%w%w%w%w);?%s*") or nil local rgb = ceil and { random(tonumber(sub(ceil, 1, 2), 16)), random(tonumber(sub(ceil, 3, 4), 16)), random(tonumber(sub(ceil, 5, 6), 16)) } or { random(255), random(255), random(255) }   local hex = "" for _, val in ipairs(rgb) do       local color = "" while (val > 0) do           local index = fmod(val, 16) + 1 val = floor(val / 16) color = string.sub('0123456789ABCDEF', index, index) .. color end if #color == 0 then color = "00" elseif #color == 1 then color = "0" .. color end hex = hex .. color end return hex end

--% This function returns the provided string with randomly dispersed capitals --@ input (string) the string to be random-ified --: output (string) the string with random capitals function p._random_caps(input) local ret = {} local wikiLinkToken = 0 for i = 1, #input do       local letter = sub(input, i, i)        if wikiLinkToken == 2 then -- Full wikilink start token reached wikiLinkToken = letter == "|" and 0 or 2 table.insert(ret, letter) elseif letter == "[" then -- Reached part of wikilink start token wikiLinkToken = wikiLinkToken + 1 table.insert(ret, letter) else -- rAnDoMIzE CApS wikiLinkToken = 0 table.insert(ret, (random > 0.5 and lower or upper)(letter)) end end return table.concat(ret) end

-- Hacky stuff to make functions available to both templates and modules create_unpack_wrapper_function = function(name, func) p[name] = utils.make_wrapper_function(function(args)        local trim = utils.trim_positional_arguments(args)        return func(#trim > 0 and trim[1] or nil)    end) end create_unpack_wrapper_function("random_color", p._random_color) create_unpack_wrapper_function("random_caps", p._random_caps) return p