Template:Choose

From aroonn wiki
Jump to navigation Jump to search
You are viewing the documentation for Template:Choose, which has been automatically transcluded from its documentation page. You may:
  • Edit the template; or:
  • Edit the documentation

The MediaWiki software (as at version 1.21) lacks a way of generating random numbers. Provided you dont need too much randomness, this template may suffice.

What it does

Returns a "pseudo-random" number between 0 and the number one less than its argument, inclusive. The default argument is 10, so the default range is 0..9 inclusive.

The resulting number can be used in a #switch: expression.

Note that the returned number is much more pseudo than random, and is not suitable for any security-related application whatsoever. In fact, the algorithm is so poor that consecutive calls with the same argument return the same value. But its better than nothing.

How it works

(You do not need to read this section to be able to use the template.)

Generates the 24-hour time; eg the time now is 184252. Treats this as a decimal integer. Reduces it modulo the argument (default 10). Returns the result.

Usage and examples

This template takes a single, unnamed, optional argument, which specifies an upper limit (the lower limit is always 0). If not supplied, this defaults to 10. Hence:

{{choose}}

- generates an integer from [0..9]; for example: 2.

You can change the range simply by adding a fixed number to the result (adjusting the argument if necessary). For example, to simulate throwing a d20, use:

{{#expr:{{choose|20}} + 1}}

- which generates an integer from [1..20]; for example: 13.

Unfortunately, it is not possible to simulate multiple dice; since the result is the same for consecutive calls, then:

{{#expr:{{choose|6}} + {{choose|6}} + 2}}

- would always have the effect of rolling doubles. If it is not important to produce the correct probability distribution, then you can of course approximate the result simply by generating an integer from [2..12]:

{{#expr:{{choose}} + 2}}