From WikiChip
Editing mirc/identifiers/$rand
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
This page supports semantic in-text annotations (e.g. "[[Is specified as::World Heritage Site]]") to build structured and queryable content provided by Semantic MediaWiki. For a comprehensive description on how to use annotations or the #ask parser function, please have a look at the getting started, in-text annotation, or inline queries help pages.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
− | {{mirc title|$rand identifier}}'''$rand''' returns a random integer from the specified range. If you supply two strings or characters not beginning with a number, it returns a random character from that range of code points. | + | {{mirc title|$rand identifier}}'''$rand''' returns a random integer from the specified range. If you supply two strings or characters not beginning with a number, it returns a random character from that range of code points. |
== Synopsis == | == Synopsis == | ||
Line 11: | Line 11: | ||
* Note: High/Low endpoints of output range can be used in either order. | * Note: High/Low endpoints of output range can be used in either order. | ||
* Note: Through v7.54 numbers must be 0 or greater. Starting v7.55 one/both number can be negative | * Note: Through v7.54 numbers must be 0 or greater. Starting v7.55 one/both number can be negative | ||
− | |||
== Properties == | == Properties == | ||
Line 17: | Line 16: | ||
== Examples == | == Examples == | ||
− | <source lang="mIRC">//echo -a $rand( | + | <source lang="mIRC">//echo -a $rand(0,10)</source> |
− | returns a random number from | + | returns a random number from 0 through 10. |
− | |||
− | |||
− | |||
<source lang="mIRC">//echo -a $rand(A,Z) | <source lang="mIRC">//echo -a $rand(A,Z) | ||
Line 43: | Line 39: | ||
== Notes == | == Notes == | ||
− | * It does not matter which value is higher, $rand(a,z) is same as $rand(z,a) | + | * It does not matter which value is higher, $rand(a,z) is same as $rand(z,a) |
− | * If either number has a fraction, it's rounded (not $int) to the nearest integer. | + | * If either value is not 0 or positive, the first character of both value is considered the starting/ending codepoint. |
− | + | * Through v7.54, if one of the numbers is negative, it's treated as if the value was the hyphen's non-numeric codepoint 45. | |
− | + | * If either number has a fraction, it's rounded (not $int) to the nearest integer. | |
* $rand(number,non-number) returns $null, so if you need ASCII range from '1' through 'Z', use $chr($rand($asc(1),$asc(Z))) | * $rand(number,non-number) returns $null, so if you need ASCII range from '1' through 'Z', use $chr($rand($asc(1),$asc(Z))) | ||
− | |||
− | |||
* $rand(foo,bar) returns codepoints from the first character of each value, 'b' through 'f' | * $rand(foo,bar) returns codepoints from the first character of each value, 'b' through 'f' | ||
− | * $rand does not claim to create cryptographically secure output. For that type of random number, you can consider using $rands and/or use up to 53 bits of the output of a $hmac digest which uses a random message and a secret key, and use $base to translate | + | * $rand does not claim to create cryptographically secure output. For that type of random number, you can consider using $rands and/or use up to 53 bits of the output of a $hmac digest which uses a random message and a secret key, and use $base to translate that string into a base 10 number. |
− | * As of v6.35, valid range was 0-999999999999 (12 9's approx 2^40), and | + | * As of v6.35, valid range was 0-999999999999 (12 9's approx 2^40), and as of v7.54 the valid range was 0 through 99999999999999 (slightly greater than 2^46.5). But a bug does not allow all values within that range to be returned. Even as low as a 24-bit range $rand(0,16777215), nearly a third of v7.54 outputs appear to be rarely or never returned. Those versions also had a modulo bias favoring lower numbers for some range sizes, which can be hard to detect except at large ranges. |
− | |||
− | |||
Demonstrating bias in v7.54: Paste the command below into any editbox, and the distribution within the range from low to high appears fairly random. However change the 14 9's into 14 6's and 2/3rds of the outputs are in the lower regions of the range. For older versions whose valid range is only as large as 12 9's, shorten the string of 9's to length 12. | Demonstrating bias in v7.54: Paste the command below into any editbox, and the distribution within the range from low to high appears fairly random. However change the 14 9's into 14 6's and 2/3rds of the outputs are in the lower regions of the range. For older versions whose valid range is only as large as 12 9's, shorten the string of 9's to length 12. | ||
Line 62: | Line 54: | ||
</source> | </source> | ||
− | + | If worried that your range is large enough to display either bias, you can use the slower randpatch alias to create more accurate output using the output from 5 smaller ranges. You can test randpatch by substituting it in place of $rand in the above command. | |
− | |||
− | If worried that your large | ||
− | |||
− | |||
<source lang="mIRC"> | <source lang="mIRC"> |