Jump to content

Module:Random/doc

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 11:52, 14 November 2013 (Number: fix missing space). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.


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 available functions are outlined in more detail below.

Number

The number function outputs a random number.

{{#invoke:random|number|m|n}}

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 . m must be positive.
  • With two arguments, returns an integer in the range . m and n can be either positive or negative; however, n must be greater than or equal to m, otherwise it is ignored and the module returns an integer in the range .

Examples (refresh)

  • {{#invoke:random|number}} → 0.61275353776885
  • {{#invoke:random|number|100}} → 98
  • {{#invoke:random|number|-100|-50}} → -66

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.

Item

The item function outputs a random item from a list.

{{#invoke:random|item|list item 1|list item 2|list item 3|...}}

Example (refresh)

  • {{#invoke:random|item|egg|beans|sausage|bacon|spam}} → spam

Randomize

The randomize function outputs a list in a random order, with an optional separator.

{{#invoke:random|item|list item 1|list item 2|list item 3|...|sep=separator}}

The separator can be specified with either the |sep= or |separator= parameters.

Possible separator values:

Code Output
dot  ·
pipe |
comma ,
tpt-languages ⧼tpt-languages-separator⧽
space a space
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 (refresh)

  • {{#invoke:random|randomize|egg|beans|sausage|bacon|spam}}Script error: The function "randomize" does not exist.
  • {{#invoke:random|randomize|egg|beans|sausage|bacon|spam|sep=dot}}Script error: The function "randomize" does not exist.
  • {{#invoke:random|randomize|egg|beans|sausage|bacon|spam|sep=space}}Script error: The function "randomize" does not exist.
  • {{#invoke:random|randomize|egg|beans|sausage|bacon|spam|sep=; }}Script error: The function "randomize" does not exist.
  • {{#invoke:random|randomize|egg|beans|sausage|bacon|spam|sep=foo}}Script error: The function "randomize" does not exist.

Randomize to text

The randomizeToText function outputs a list in a random order, text-style. In other words, it is like the randomize function, but with a different separator before the last item.

{{#invoke:random|item|list item 1|list item 2|list item 3|...|sep=separator|conj=conjunction}}

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 randomize function.

  • {{#invoke:random|randomizeToText|egg|beans|sausage|bacon|spam}}Script error: The function "randomizeToText" does not exist.
  • {{#invoke:random|randomizeToText|egg|beans|sausage|bacon|spam|sep=; }}Script error: The function "randomizeToText" does not exist.
  • {{#invoke:random|randomizeToText|egg|beans|sausage|bacon|spam|sep=; |conj= or }}Script error: The function "randomizeToText" does not exist.