Module:Random
![]() | This Lua module is used on approximately 13,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This module depends on the following other modules: |
This module contains a number of functions that use random numbers. It can output random numbers, select a random item from a list, and reorder lists randomly. The randomly reordered lists can be output inline, or as various types of ordered and unordered lists. The available functions are outlined in more detail below.
Number
The number
function outputs a random number.
{{#invoke:random|number|m|n|same=yes}}
The arguments m
and n
may be omitted, but if specified must be convertible to integers.
- With no arguments, returns a real number in the range .
- With one argument, returns an integer in the range , or, if
m
is negative, . Ifm
is equal to 0 or 1, returns 1 (or 0 if ). - With two arguments, returns an integer in the range .
m
andn
can be either positive or negative. Ifm
is greater thann
, returns an integer in the range instead. - If the
|same=
parameter is set to "yes", "y", "true", or "1", the same random number is returned for each module call on a given page.
Examples (
){{#invoke:random|number}}
→ 0.25897995534305{{#invoke:random|number|100}}
→ Lua error at line 50: attempt to compare number with nil.{{#invoke:random|number|-100|-50}}
→ -87{{#invoke:random|number|100|same=yes}}
→ Lua error at line 50: attempt to compare number with nil.{{#invoke:random|number|100|same=yes}}
→ Lua error at line 50: attempt to compare number with nil.
The documentation for this function is partly taken from the Scribunto Lua reference manual, which is in turn based on the Lua 5.1 Reference Manual, available under the MIT License.
Date
The date
function outputs a random date.
{{#invoke:random|date|timestamp1|timestamp2|format=date format|same=yes}}
- If no timestamp arguments are specified, the module outputs a random date in the current year.
- If
timestamp1
andtimestamp2
are specified, the module outputs a random date between the two timestamps.timestamp1
must be earlier thantimestamp2
. - If only
timestamp1
is specified, the module outputs a random date between the Unix epoch (1 Jan 1970) and the timestamp.timestamp1
must not be earlier than 1 Jan 1970. - Formatting can be specified with the
|format=
parameter. The default formatting is "hh:mm, DD Month YYYY (UTC)" (the same as the default Wikipedia timestamp). - The timestamps and the
|format=
parameter accept values compatible with the #time parser function. Please see the #time documentation for the full range of possible input values and formatting options. - If the
|same=
parameter is set to "yes", "y", "true", or "1", the same date is returned for each module call on a given page.
Examples (
){{#invoke:random|date}}
→ Script error: The function "date" does not exist.{{#invoke:random|date|format=F j}}
→ Script error: The function "date" does not exist.{{#invoke:random|date|1 Jan 1980|31 Dec 1999}}
→ Script error: The function "date" does not exist.{{#invoke:random|date|1st January 1500|1st January 3000|format=g:i a, l d M Y}}
→ Script error: The function "date" does not exist.{{#invoke:random|date|1970/06/01}}
→ Script error: The function "date" does not exist.{{#invoke:random|date|same=yes}}
→ Script error: The function "date" does not exist.{{#invoke:random|date|same=yes}}
→ Script error: The function "date" does not exist.
Item
The item
function outputs a random item from a list.
{{#invoke:random|item|list item 1|list item 2|list item 3|...|same=yes}}
If the |same=
parameter is set to "yes", "y", "true", or "1", the same item is returned for each module call on a given page.
Example (
){{#invoke:random|item|egg|beans|sausage|bacon|spam}}
→ beans{{#invoke:random|item|egg|beans|sausage|bacon|spam|same=yes}}
→ beans{{#invoke:random|item|egg|beans|sausage|bacon|spam|same=yes}}
→ beans
Setting "same" will make the template select the same number item from each list, even if the things in them are different.
My {{#invoke:random|item|egg |bean |sausage |bacon |spam |same=yes}} is {{#invoke:random|item|quail |pinto |kielbasa |canadian |spammy|same=yes}} and my {{#invoke:random|item|blegg |blean |blausage |blacon |blam |same=yes}} is {{#invoke:random|item|blail |blinto|blielbasa|blanadian|blammy|same=yes}}
→ My bean is pinto and my blean is blinto.
List
The list
function outputs a list in a random order.
{{#invoke:random|list|list item 1|list item 2|list item 3|...|sep=separator|limit=number of items to display|same=yes}}
Named parameters
|sep=
or|separator=
- an optional separator for the list items. Some values are special; see the table below.|limit=
- the maximum number of list items to display. The lowest possible is 0 and the highest possible is the length of the list.|same=
- if this is set to "yes", "y", "true", or "1", the list order is the same for each module call on a given page.
Code | Output |
---|---|
dot |
· |
pipe
|
| |
comma |
, |
tpt-languages |
⧼tpt-languages-separator⧽ |
space |
a space |
newline |
a newline character |
any other value | other values are used without modification |
You cannot input spaces directly to the |sep=
parameter due to limitations in MediaWiki's template syntax. However, it is possible to work around this by using HTML entities. You can use  
to represent a normal space, and
to represent a non-breaking space.
Examples (
){{#invoke:random|list|egg|beans|sausage|bacon|spam}}
→ Script error: The function "list" does not exist.{{#invoke:random|list|egg|beans|sausage|bacon|spam|sep=dot}}
→ Script error: The function "list" does not exist.{{#invoke:random|list|egg|beans|sausage|bacon|spam|sep=space}}
→ Script error: The function "list" does not exist.{{#invoke:random|list|egg|beans|sausage|bacon|spam|sep=; }}
→ Script error: The function "list" does not exist.{{#invoke:random|list|egg|beans|sausage|bacon|spam|sep=foo}}
→ Script error: The function "list" does not exist.{{#invoke:random|list|egg|beans|sausage|bacon|spam|limit=3}}
→ Script error: The function "list" does not exist.{{#invoke:random|list|egg|beans|sausage|bacon|spam|same=yes}}
→ Script error: The function "list" does not exist.{{#invoke:random|list|egg|beans|sausage|bacon|spam|same=yes}}
→ Script error: The function "list" does not exist.
Text list
The text_list
function outputs a list in a random order, text-style. In other words, it is like the list
function, but with a different separator before the last item.
{{#invoke:random|text_list|list item 1|list item 2|list item 3|...|sep=separator|conj=conjunction|limit=number of items to display|same=yes}}
The separator can be specified with either the |sep=
or |separator=
parameters; its default value is ", ". The conjunction can be specified with either the |conj=
or |conjunction=
parameters; its default value is " and ". The separator and the conjunction can be specified with the same values as the separator in the list function.
The maximum number of list items to display can be set with the |limit=
parameter. The lowest possible is 0 and the highest possible is the length of the list.
If the |same=
parameter is set to "yes", "y", "true", or "1", the list order is the same for each module call on a given page.
Examples (
){{#invoke:random|text_list|egg|beans|sausage|bacon|spam}}
→ Script error: The function "text_list" does not exist.{{#invoke:random|text_list|egg|beans|sausage|bacon|spam|sep=; }}
→ Script error: The function "text_list" does not exist.{{#invoke:random|text_list|egg|beans|sausage|bacon|spam|sep=; |conj= or }}
→ Script error: The function "text_list" does not exist.{{#invoke:random|text_list|egg|beans|sausage|bacon|spam|limit=3}}
→ Script error: The function "text_list" does not exist.{{#invoke:random|text_list|egg|beans|sausage|bacon|spam|same=yes}}
→ Script error: The function "text_list" does not exist.{{#invoke:random|text_list|egg|beans|sausage|bacon|spam|same=yes}}
→ Script error: The function "text_list" does not exist.
HTML lists
If you wish to output an HTML list in a random order, you can choose between five different functions: bulleted_list
, unbulleted_list
, horizontal_list
, ordered_list
, and horizontal_ordered_list
. These functions all use Module:List.
Function name | Produces | Example code | Example output ( | )
---|---|---|---|
bulleted_list
|
Bulleted lists | {{#invoke:random|bulleted_list|egg|sausage|spam}}
|
Script error: The function "bulleted_list" does not exist. |
unbulleted_list
|
Unbulleted lists | {{#invoke:random|unbulleted_list|egg|sausage|spam}}
|
Script error: The function "unbulleted_list" does not exist. |
horizontal_list
|
Horizontal bulleted lists | {{#invoke:random|horizontal_list|egg|sausage|spam}}
|
Script error: The function "horizontal_list" does not exist. |
ordered_list
|
Ordered lists (numbered lists and alphabetical lists) | {{#invoke:random|ordered_list|egg|sausage|spam}}
|
Script error: The function "ordered_list" does not exist. |
horizontal_ordered_list
|
Horizontal ordered lists | {{#invoke:random|horizontal_ordered_list|egg|sausage|spam}}
|
Script error: The function "horizontal_ordered_list" does not exist. |
- Basic usage
{{#invoke:random|function|list item 1|list item 2|list item 3|...|limit=number of items to display|same=yes}}
- All parameters
{{#invoke:random|function |first item|second item|third item|... |start = start number for ordered lists |type = type of marker for ordered lists |list_style_type = type of marker for ordered lists (uses CSS) |class = class |style = style |list_style = style for the list |item_style = style for all list items |item_style1 = style for the first list item |item_style2 = style for the second list item |... |indent = indent for horizontal lists }}
The maximum number of list items to display can be set with the |limit=
parameter. The lowest possible is 0 and the highest possible is the length of the list.
If the |same=
parameter is set to "yes", "y", "true", or "1", the list order is the same for each module call on a given page.
Please see Module:List for a full explanation of the other parameters.
-- This module contains a number of functions that make use of random numbers.
local cfg = {}
cfg.space = 'space'
local p = {}
-- Set the seed for the random number to the current number of edits made to Wikipedia.
-- The English Wikipedia gets dozens of edits each minute, so this is as close to a random seed
-- as we have. On smaller wikis this will produce the same number if the edit count has not changed,
-- so please use it with caution.
math.randomseed(mw.site.stats.edits)
local function removeBlanks(t)
-- Removes blank entries from an array so that it can be used with ipairs.
local ret = {}
for k, v in pairs(t) do
if type(k) == 'number' then -- Make sure we have no non-string portal names.
table.insert(ret, k)
end
end
table.sort(ret)
for i, v in ipairs(ret) do
ret[i] = t[v]
end
return ret
end
local function makeSeparator(sep)
if sep == cfg.space then
-- Include an easy way to use spaces as separators.
return ' '
elseif type(sep) == 'string' then
-- If the separator is a recognised MediaWiki separator, use that. Otherwise use the value of sep if it is a string.
local mwseparators = {'dot', 'pipe', 'comma', 'tpt-languages'}
for _, mwsep in ipairs(mwseparators) do
if sep == mwsep then
return mw.message.new( sep .. '-separator' ):plain()
end
end
return sep
end
end
function p._number(args)
-- Returns a random number. This algorithm is taken from [[Module:Math]].
first = tonumber(args[1]) -- if it doesn't exist it's NaN, if not a number it's nil
second = tonumber(args[2])
if first then -- if NaN or nil, will skip down to final return
if first <= second then -- could match if both nil, but already checked that first is a number in last line
return math.random(first, second)
end
return math.random(first)
end
return math.random()
end
function p._item(args)
-- Returns a random item from a numbered list.
local list = removeBlanks(args)
if #list >= 1 then
return list[math.random(#list)]
end
end
function p._randomize(args)
-- Randomizes a list and concatenates the result with a separator. It works by iterating through the list backwards,
-- 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
local function makeWrapper(func)
return function (frame)
-- If called via #invoke, use the args passed into the invoking template, or the args passed to #invoke if any exist.
-- Otherwise assume args are being passed directly in from the debug console or from another Lua module.
local origArgs
if frame == mw.getCurrentFrame() then
origArgs = frame:getParent().args
for k, v in pairs(frame.args) do
origArgs = frame.args
break
end
else
origArgs = frame
end
-- Trim whitespace and remove blank arguments.
local args = {}
for k, v in pairs(origArgs) do
if type(v) == 'string' then
v = mw.text.trim(v)
end
if v ~= '' then
args[k] = v
end
end
return p[func](args)
end
end
for funcName in pairs(p) do
-- Generates a function to be used with #invoke, removing the leading underscore.
-- For example, p._number will be accessible to #invoke using p.number.
p[funcName:sub(2, -1)] = makeWrapper(funcName)
end
return p