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. $r is an equivalent alias for $rand
+
{{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
* Note: v6.35 returns range as large as 12 9's. Somewhere between v7.1 and 7.54 it changed to allow range 14 9's. Beginning v7.55, range can now be 2^53. Beginning v7.55 $rand returns output from Bob Jenkins' jsf64.
 
  
 
== Properties ==
 
== Properties ==
Line 17: Line 16:
  
 
== Examples ==
 
== Examples ==
<source lang="mIRC">//echo -a $rand(1,100)</source>
+
<source lang="mIRC">//echo -a $rand(0,10)</source>
returns a random number from 1 through 100 inclusive.
+
returns a random number from 0 through 10.
 
 
<source lang="mIRC">//echo -a $rand(-5,20)</source>
 
returns a random number from negative 5 through positive 20.
 
  
 
<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) and $rand(9,1) is same as $rand(1,9)
+
* 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. $rand(-1.6,1.6) is $rand(-2,2)
+
* If either value is not 0 or positive, the first character of both value is considered the starting/ending codepoint.
* 'number' is allowed to have stripped trailing non-numerics only if neither number is negative
+
* 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.
* stripped non-numerics sees to follow same rules as $int() does. $rand(1abc,1e2abc) is $rand(1,100)
+
* 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)))
* The first character of text strings is used as 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.
 
 
* $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 up to 53 bits of that string into a base 10 number.
+
* $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 somewhere prior to v7.52 the valid range was increased to be 14 9's, 0 through 99999999999999 (slightly greater than 2^46.5). But a bug did 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 or with large number of test outputs.
+
* 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.
* Random does not mean 'equal outcomes'. While it's more likely to have 100 6's from 600 dice rolls than any other individual total-result, only a small portion of groups of 600 rolls would have 100 6's. It is possible, though extremely unlikely, for a 'fair' random $rand(1,6) to return 10 6's in a row. Unless the prior results were caused by a bias in the number generator, the odds of the 11th roll being a 6 remains 1/6th.
 
* The next 64-bit value output by the jsf64 number generator is 100% determined by the unknown state of the 4 64-bit variables of the RNG state, which can be any of 2^256 possible states. There should be approx 2^192 different states which could have returned the latest 64-bit value, and 2^128 possible states returning that exact combo of 64bit values. This RNG should not have a detectible bias favoring some bits being 0's or 1's, or some numbers being more/less likely when following/preceding certain other numbers. The value appears random due to lack of information needed to know the next number.
 
  
 
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>
  
In v7.55+ the above alias can increase %max to $calc(2^53-1) without obvious bias.<br />
+
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 range has some 'impossible' outputs, you can use the slower randpatch alias to create output which combines the output from 5 smaller ranges. You can test randpatch by substituting it in place of $rand in the above command.
 
 
 
The %throwaway_above calculation discards the highest values which caused modulo bias when shrinking the outputs down to the requested range, preventing 1 or more of the lowest outputs from having an extra input when allocating the 2^53 possible inputs among the possible outputs of the requested range.  
 
  
 
<source lang="mIRC">
 
<source lang="mIRC">

Please note that all contributions to WikiChip may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see WikiChip:Copyrights for details). Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)