Module:Random/doc

Revision as of 21:52, 26 October 2026 by Limeee (talk | contribs) (copy from English Wikipedia)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This is the documentation page for Module:Random

Template:High-use Template:Module rating Template:Lua 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

edit

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 <math>[0,1)</math>.
  • With one argument, returns an integer in the range <math>[1,m]</math>, or, if m is negative, <math>(-m,0]</math>. If m is equal to 0 or 1, returns 1 (or 0 if <math>m=-1</math>).
  • With two arguments, returns an integer in the range <math>[m,n]</math>. m and n can be either positive or negative. If m is greater than n, returns an integer in the range <math>[n,m]</math> instead.
  • If the Template:Para parameter is set to "yes", "y", "true", or "1", the same random number is returned for each module call on a given page.

Examples (Template:Purge)

  • <syntaxhighlight lang="wikitext" inline>0.34806954550933</syntaxhighlight> → 0.21542652985799
  • <syntaxhighlight lang="wikitext" inline>16</syntaxhighlight> → 73
  • <syntaxhighlight lang="wikitext" inline>-62</syntaxhighlight> → -83
  • <syntaxhighlight lang="wikitext" inline>47</syntaxhighlight> → 47
  • <syntaxhighlight lang="wikitext" inline>47</syntaxhighlight> → 47

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

edit

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 and timestamp2 are specified, the module outputs a random date between the two timestamps. timestamp1 must be earlier than timestamp2.
  • If only timestamp1is 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 Template:Para parameter. The default formatting is "hh:mm, DD Month YYYY (UTC)" (the same as the default Wikipedia timestamp).
  • The timestamps and the Template:Para 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 Template:Para parameter is set to "yes", "y", "true", or "1", the same date is returned for each module call on a given page.

Examples (Template:Purge)

  • <syntaxhighlight lang="wikitext" inline>01:55, 18 September 2026 (UTC)</syntaxhighlight> → 05:44, 18 August 2026 (UTC)
  • <syntaxhighlight lang="wikitext" inline>December 30</syntaxhighlight> → April 17
  • <syntaxhighlight lang="wikitext" inline>21:00, 01 August 1992 (UTC)</syntaxhighlight> → 08:19, 23 April 1989 (UTC)
  • <syntaxhighlight lang="wikitext" inline>8:26 am, Tuesday 21 Sep 1537</syntaxhighlight> → 5:04 am, Tuesday 04 Jan 1820
  • <syntaxhighlight lang="wikitext" inline>05:41, 12 March 1970 (UTC)</syntaxhighlight> → 06:07, 12 April 1970 (UTC)
  • <syntaxhighlight lang="wikitext" inline>01:58, 21 June 2026 (UTC)</syntaxhighlight> → 01:58, 21 June 2026 (UTC)
  • <syntaxhighlight lang="wikitext" inline>01:58, 21 June 2026 (UTC)</syntaxhighlight> → 01:58, 21 June 2026 (UTC)

Item

edit

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 Template:Para parameter is set to "yes", "y", "true", or "1", the same item is returned for each module call on a given page.

Example (Template:Purge)

  • <syntaxhighlight lang="wikitext" inline>egg</syntaxhighlight> → beans
  • <syntaxhighlight lang="wikitext" inline>spam</syntaxhighlight> → spam
  • <syntaxhighlight lang="wikitext" inline>spam</syntaxhighlight> → spam

Setting "same" will make the template select the same number item from each list, even if the things in them are different.

  • <syntaxhighlight lang="wikitext">My

spam is spammy and my blam is blammy</syntaxhighlight>

→ My spam is spammy and my blam is blammy.

List

edit

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

  • Template:Para or Template:Para - an optional separator for the list items. Some values are special; see the table below.
  • Template:Para - the maximum number of list items to display. The lowest possible is 0 and the highest possible is the length of the list.
  • Template:Para - if this is set to "yes", "y", "true", or "1", the list order is the same for each module call on a given page.
Possible separator values
Code Output
dot ⧼dot-separator⧽
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 Template:Para parameter due to limitations in MediaWiki's template syntax. However, it is possible to work around this by using HTML entities. You can use &#32; to represent a normal space, and &nbsp; to represent a non-breaking space.

Examples (Template:Purge)

  • <syntaxhighlight lang="wikitext" inline>sausageeggbaconspambeans</syntaxhighlight> → eggspambaconsausagebeans
  • <syntaxhighlight lang="wikitext" inline>bacon⧼dot-separator⧽sausage⧼dot-separator⧽beans⧼dot-separator⧽spam⧼dot-separator⧽egg</syntaxhighlight> → spam⧼dot-separator⧽egg⧼dot-separator⧽sausage⧼dot-separator⧽beans⧼dot-separator⧽bacon
  • <syntaxhighlight lang="wikitext" inline>spam sausage beans bacon egg</syntaxhighlight> → egg sausage bacon spam beans
  • <syntaxhighlight lang="wikitext" inline>bacon;&#32;egg;&#32;beans;&#32;spam;&#32;sausage</syntaxhighlight> → beans; bacon; spam; sausage; egg
  • <syntaxhighlight lang="wikitext" inline>eggfoobeansfoobaconfoosausagefoospam</syntaxhighlight> → eggfoobaconfoospamfoobeansfoosausage
  • <syntaxhighlight lang="wikitext" inline>sausageeggspam</syntaxhighlight> → spamsausageegg
  • <syntaxhighlight lang="wikitext" inline>eggbaconsausagebeansspam</syntaxhighlight> → eggbaconsausagebeansspam
  • <syntaxhighlight lang="wikitext" inline>eggbaconsausagebeansspam</syntaxhighlight> → eggbaconsausagebeansspam

Text list

edit

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 Template:Para or Template:Para parameters; its default value is ", ". The conjunction can be specified with either the Template:Para or Template:Para 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 Template:Para parameter. The lowest possible is 0 and the highest possible is the length of the list.

If the Template:Para parameter is set to "yes", "y", "true", or "1", the list order is the same for each module call on a given page.

Examples (Template:Purge)

  • <syntaxhighlight lang="wikitext" inline>egg, sausage, beans, spam and bacon</syntaxhighlight> → bacon, egg, beans, spam and sausage
  • <syntaxhighlight lang="wikitext" inline>beans;&#32;egg;&#32;bacon;&#32;spam and sausage</syntaxhighlight> → spam; beans; sausage; egg and bacon
  • <syntaxhighlight lang="wikitext" inline>bacon;&#32;egg;&#32;beans;&#32;spam&#32;or&#32;sausage</syntaxhighlight> → beans; egg; sausage; bacon or spam
  • <syntaxhighlight lang="wikitext" inline>sausage, egg and beans</syntaxhighlight> → spam, bacon and egg
  • <syntaxhighlight lang="wikitext" inline>egg, bacon, sausage, beans and spam</syntaxhighlight> → egg, bacon, sausage, beans and spam
  • <syntaxhighlight lang="wikitext" inline>egg, bacon, sausage, beans and spam</syntaxhighlight> → egg, bacon, sausage, beans and spam

HTML lists

edit

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 (Template:Purge)
bulleted_list Bulleted lists <syntaxhighlight lang="wikitext" inline>
  • egg
  • spam
  • sausage
</syntaxhighlight>
  • egg
  • sausage
  • spam
unbulleted_list Unbulleted lists <syntaxhighlight lang="wikitext" inline>Page Template:Plainlist/styles.css has no content.
  • sausage
  • spam
  • egg
</syntaxhighlight>
Page Template:Plainlist/styles.css has no content.
  • spam
  • egg
  • sausage
horizontal_list Horizontal bulleted lists <syntaxhighlight lang="wikitext" inline>Page Template:Hlist/styles.css has no content.
  • sausage
  • spam
  • egg
</syntaxhighlight>
Page Template:Hlist/styles.css has no content.
  • egg
  • sausage
  • spam
ordered_list Ordered lists (numbered lists and alphabetical lists) <syntaxhighlight lang="wikitext" inline>
  1. spam
  2. sausage
  3. egg
</syntaxhighlight>
  1. egg
  2. sausage
  3. spam
horizontal_ordered_list Horizontal ordered lists <syntaxhighlight lang="wikitext" inline>Page Template:Hlist/styles.css has no content.
  1. sausage
  2. spam
  3. egg
</syntaxhighlight>
Page Template:Hlist/styles.css has no content.
  1. egg
  2. spam
  3. sausage
Basic usage
{{#invoke:random|function|list item 1|list item 2|list item 3|...|limit=number of items to display|same=yes}}
All parameters

Template:Pre

The maximum number of list items to display can be set with the Template:Para parameter. The lowest possible is 0 and the highest possible is the length of the list.

If the Template:Para 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.