Content deleted Content added
add limit parameter for lists, and optimise the p.item code slightly |
use args.same instead of a same_number function, add a config option to set a good seed for wikis with a low edit rate |
||
Line 1:
-- This module contains a number of functions that make use of random numbers.
local
--------------------------------------------------------------------------------------
-- Configuration
--------------------------------------------------------------------------------------
-- Set this to true if your wiki has a traffic rate of less than one edit every two minutes or so.
-- This will prevent the same "random" number being generated many times in a row until a new edit is made
-- to the wiki. This setting is only relevant if the |same= parameter is set.
cfg.lowTraffic = false
-- If cfg.lowTraffic is set to true, and the |same= parameter is set, this value is used for the refresh rate of the random seed.
-- This is the number of seconds until the seed is changed. Getting this right is tricky. If you set it too high, the same number
-- will be returned many times in a row. If you set it too low, you may get different random numbers appearing on the same page,
-- particularly for pages that take many seconds to process.
cfg.seedRefreshRate = 60
--------------------------------------------------------------------------------------
-- End configuration
--------------------------------------------------------------------------------------
local p = {} -- For functions available from other Lua modules.
local l = {} -- For functions not available from other Lua modules, but that need to be accessed using table keys.
local yesno = require('Module:Yesno')
local makeList = require('Module:List').makeList
Line 8 ⟶ 31:
-- Helper functions
--------------------------------------------------------------------------------------
-- Set the seed for the random number generator. This works well on the English Wikipedia due to the high▼
math.randomseed(mw.site.stats.edits + mw.site.stats.pages + os.time() + math.floor(os.clock() * 1000000000))▼
end▼
local function raiseError(msg)
Line 36 ⟶ 52:
end
-- Gets a random number
first = tonumber(args[1])
second = tonumber(args[2])
Line 53 ⟶ 69:
return math.random()
end
end▼
function p._number(args)▼
end▼
local stats = mw.site.stats▼
local views = stats.views or 0 -- This is not always available, so we need a backup.▼
local seed = views + stats.pages + stats.articles + stats.files + stats.edits + stats.users + stats.activeUsers + stats.admins -- Make this as random as possible without using os.time() or os.clock()▼
math.randomseed(seed)▼
end
Line 83 ⟶ 75:
--------------------------------------------------------------------------------------
function
-- This function gets random dates, and takes timestamps as positional arguments.
-- With no arguments specified, it outputs a random date in the current year.
Line 90 ⟶ 82:
-- The output can be formatted using the "format" argument, which works in the same way as the #time parser function.
-- The default format is the standard Wikipedia timestamp.
local lang = mw.language.getContentLanguage()
Line 154 ⟶ 144:
--------------------------------------------------------------------------------------
local function
-- Randomizes an array. It works by iterating through the list backwards, each time swapping the entry
-- "i" with a random entry. Courtesy of Xinhuan at http://forums.wowace.com/showthread.php?p=279756
-- If the limit parameter is set, the array is shortened to that many elements after being randomized.
-- The lowest possible value is 0, and the highest possible is the length of the array.
local len = #t
for i = len, 2, -1 do
Line 212 ⟶ 201:
local function makeRandomList(args)
local list = removeBlanks(args)
list =
return list
end
function
-- Returns a random item from a numbered list.
local list = removeBlanks(args)
local len = #list
Line 226 ⟶ 214:
end
function
-- Randomizes a list and concatenates the result with a separator.
local list = makeRandomList(args)
local sep = makeSeparator(args.sep or args.separator)
Line 234 ⟶ 221:
end
function
-- Randomizes a list and concatenates the result, text-style. Accepts separator and conjunction arguments.
local list = makeRandomList(args)
local sep = makeSeparator(args.sep or args.separator)
local conj = makeSeparator(args.conj or args.conjunction)
return mw.text.listToText(list, sep, conj)
▲end
-- Returns a Lua array, randomized. For use from other Lua modules.
return randomizeArray(args.t, args.limit)
end
Line 247 ⟶ 238:
--------------------------------------------------------------------------------------
function
-- Randomizes a list and turns it into an HTML list. Uses [[Module:List]].
listType = listType or 'bulleted'
local listArgs = makeRandomList(args) -- Arguments for [[Module:List]].
Line 260 ⟶ 250:
end
--------------------------------------------------------------------------------------
-- The main function. Called from other Lua modules.
--------------------------------------------------------------------------------------
function p.main(funcName, args, listType)
▲ --
local same = yesno(args.same)
if not same then
-- Generates a different number every time the module is called, even from the same page.
-- This is because of the variability of os.clock (the time in seconds that the Lua script has been running for).
▲
else
if not cfg.lowTraffic then
-- Make the seed as random as possible without using anything time-based. This means that the same random number
-- will be generated for the same input from the same page - necessary behaviour for some wikicode templates that
-- assume bad pseudo-random-number generation.
▲
else
-- Make the random seed change every n seconds, where n is set by cfg.seedRefreshRate.
-- This is useful for low-traffic wikis where new edits may not happen very often.
math.randomseed(math.floor(os.time() / cfg.seedRefreshRate))
end
▲ end
if type(args) ~= 'table' then
error('the second argument to p.main must be a table')
end
return l[funcName](args, listType)
▲end
--------------------------------------------------------------------------------------
-- Process arguments from #invoke
Line 290 ⟶ 312:
end
end
return p
end
end
Line 307 ⟶ 329:
-- Process arguments for other functions.
local otherFuncs = {'number
for _, funcName in ipairs(otherFuncs) do
p[funcName] = makeWrapper(
end
|