Template:RandomChoice: Difference between revisions
GreytideSkye (talk | contribs) mNo edit summary |
GreytideSkye (talk | contribs) (Documentation and proof of function. The devil infests our wiki.) |
||
Line 1: | Line 1: | ||
{{{ {{#expr: {{#time:U}} mod {{#expr: | {{{ {{#expr: 1 + {{#time:U}} mod {{#expr: | ||
+{{#if:{{{1|}}}|1|0}} | +{{#if:{{{1|}}}|1|0}} | ||
+{{#if:{{{2|}}}|1|0}} | +{{#if:{{{2|}}}|1|0}} | ||
Line 11: | Line 11: | ||
+{{#if:{{{10|}}}|1|0}} | +{{#if:{{{10|}}}|1|0}} | ||
}} }} }}} | |||
}} }} }}}<noinclude> | |||
Behold, great syntactic sin: {{RandomChoice|First choice!|Second choice.|Third choice!!}} | |||
The math takes the current unix time, modulus the number of parameters given, and returns {{{ that number }}} which thusly returns that numbered parameter. One is added because parameters are one-indexed and modulus returns start at 0. | |||
Of note, the unix time is taken when the page loads, so if you have several random choices on a page, they all line up. We'd need a different extension to get an actual psuedo-random number, but at that point we're doing flat-out coding in our wiki and there's no way in hell that's gonna fly. | |||
See: | |||
Should be the same letter: {{RandomChoice|a|b|c|d|e}}=={{RandomChoice|A|B|C|D|E}} | |||
might be different: {{RandomChoice|a|b|c|d|e}} != {{RandomChoice|a|b|c|d|e|F}} | |||
Works for up to 10 parameters but could easily be extended to 20, 30. I think there's a hard limit of 50 but we are NOT reaching that. Nope nope nope! Likewise, I'm not going to try to handle a zero-parameter case. It returns a big red "Division by zero." as you likely see above, and that should be good enough. | |||
Parameter count expression taken from https://www.mediawiki.org/wiki/Template:Number_of_defined_parameters . | |||
God help us all. --[[User:GreytideSkye|GreytideSkye]] ([[User talk:GreytideSkye|talk]]) 17:28, 14 September 2024 (UTC) | |||
</noinclude> |
Revision as of 17:28, 14 September 2024
{{{ Division by zero. }}}
Behold, great syntactic sin: First choice!
The math takes the current unix time, modulus the number of parameters given, and returns {{{ that number }}} which thusly returns that numbered parameter. One is added because parameters are one-indexed and modulus returns start at 0.
Of note, the unix time is taken when the page loads, so if you have several random choices on a page, they all line up. We'd need a different extension to get an actual psuedo-random number, but at that point we're doing flat-out coding in our wiki and there's no way in hell that's gonna fly.
See:
Should be the same letter: a==A
might be different: a != a
Works for up to 10 parameters but could easily be extended to 20, 30. I think there's a hard limit of 50 but we are NOT reaching that. Nope nope nope! Likewise, I'm not going to try to handle a zero-parameter case. It returns a big red "Division by zero." as you likely see above, and that should be good enough.
Parameter count expression taken from https://www.mediawiki.org/wiki/Template:Number_of_defined_parameters .
God help us all. --GreytideSkye (talk) 17:28, 14 September 2024 (UTC)