From WikiChip
Difference between revisions of "mirc/identifiers/$rand"
< mirc‎ | identifiers

m
(Add'l details)
Line 1: Line 1:
{{mirc title|$rand identifier}}'''$rand''' returns a random number between v1 and v2. If you supply a value which is not a number, it uses its code point value as the number.
+
{{mirc title|$rand identifier}}'''$rand''' returns a random integer from value1 through value2. If you supply a value which is not a number, it uses its code point value as the number.
  
 
'''Note:''' This replaces the deprecated '''$r''' identifier.
 
'''Note:''' This replaces the deprecated '''$r''' identifier.
 
  
 
== Synopsis ==
 
== Synopsis ==
Line 8: Line 7:
  
 
== Parameters ==
 
== Parameters ==
'''value1''' = the starting value
+
'''value1''' = the starting value<br />
 
 
 
'''value2''' = the ending value
 
'''value2''' = the ending value
  
Line 18: Line 16:
 
<source lang="mIRC">//echo -a $rand(0,10)</source>
 
<source lang="mIRC">//echo -a $rand(0,10)</source>
  
will return a random number between 0 up to 10.
+
will return a random number from 0 through 10.
 +
 
 +
<source lang="mIRC">//echo -a $rand(A,Z)
 +
will return a random upper case from A through Z
 +
//echo -a $rand(A,z)
 +
will return upper and lower case letters, but also other ASCII characters in that range including [ \ ] ^ _ `</source>
 +
 
 +
<source lang="mIRC">
 +
Random number used as index:
 +
//echo -a Monty Hall opens Door number 2 to reveal a $gettok(goat car goat,$rand(1,3),32)
 +
 
 +
display all unique values returned within 10K random values:
 +
//var %i 10000 | var %s | while (%i) { var %a $rand($chr(33),$chr(126)) | if (%a !isincs %s) var %s %s %a | dec %i } | echo -a $sorttok(%s,32)
 +
 
 +
Displays frequency distribution. Random does not mean equal outcomes. It's rare for a dice to roll exactly 10K 6's in 60K rolls:
 +
//var %i = 60000 , %low 1 , %high 6 | var %nums $str(0 $chr(32),$calc(1+%high -%low)) | var %ticks $ticks | while (%i) { var %a $rand(%low,%high) | var %t $calc(1+%a -%low) | var %b $gettok(%nums,%t,32) | inc %b | var %nums $puttok(%nums,%b,%t,32) | dec %i } | var %c 1 | var %i %low | while ($gettok(%nums,%c,32) != $null) { var %b $gettok(%nums,%c,32) | var %nums $puttok(%nums,$+(%i,:,%b),%c,32) | inc %i | inc %c } | echo -a time: $+ $calc($ticks - %ticks) $+ ms-> %nums
 +
 
 +
Valid codepoints are 1 through 65535:
 +
//var %i 10000 , %s , %low 9100 , %high $calc(%low +999) | while (%i) { var %a $rand($chr(%low),$chr(%high)) | if (%a !isincs %s) var %s %s %a | dec %i } | echo -a $numtok(%s,32) tokens: $sorttok(%s,32)
 +
</source>
 +
 
 +
== Notes ==
  
<source lang="mIRC">//echo -a $rand(A,Z)</source>
+
* Does not matter which value is higher, $rand(a,z) is same as $rand(z,a)
will return a random upper case between A and Z.
+
* If either value is not 0 or positive, the first character of both value is considered the starting/ending codepoint.
 +
* If one of the numbers is negative, it's treated as if the value was the hyphen's codepoint 45.
 +
* If either number has a fraction, it's rounded 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(foo,bar) returns codepoints from the first character of each value, 'b' through 'f'
  
 
== Compatibility ==
 
== Compatibility ==
Line 28: Line 51:
 
== See also ==
 
== See also ==
 
* {{mIRC|$r}}
 
* {{mIRC|$r}}
* {{mIRC|$v1}}
 
* {{mIRC|$v2}}
 
 
{{mIRC identifier list}}
 
{{mIRC identifier list}}
 
[[Category:mIRC identifiers|rand]]
 
[[Category:mIRC identifiers|rand]]

Revision as of 23:59, 8 December 2017

$rand returns a random integer from value1 through value2. If you supply a value which is not a number, it uses its code point value as the number.

Note: This replaces the deprecated $r identifier.

Synopsis

$rand(value1,value2)

Parameters

value1 = the starting value
value2 = the ending value

Properties

None

Examples

//echo -a $rand(0,10)

will return a random number from 0 through 10.

//echo -a $rand(A,Z)
will return a random upper case from A through Z
//echo -a $rand(A,z)
will return upper and lower case letters, but also other ASCII characters in that range including [ \ ] ^ _ `
Random number used as index:
//echo -a Monty Hall opens Door number 2 to reveal a $gettok(goat car goat,$rand(1,3),32)
 
display all unique values returned within 10K random values:
//var %i 10000 | var %s | while (%i) { var %a $rand($chr(33),$chr(126)) | if (%a !isincs %s) var %s %s %a | dec %i } | echo -a $sorttok(%s,32)
 
Displays frequency distribution. Random does not mean equal outcomes. It's rare for a dice to roll exactly 10K 6's in 60K rolls:
//var %i = 60000 , %low 1 , %high 6 | var %nums $str(0 $chr(32),$calc(1+%high -%low)) | var %ticks $ticks | while (%i) { var %a $rand(%low,%high) | var %t $calc(1+%a -%low) | var %b $gettok(%nums,%t,32) | inc %b | var %nums $puttok(%nums,%b,%t,32) | dec %i } | var %c 1 | var %i %low | while ($gettok(%nums,%c,32) != $null) { var %b $gettok(%nums,%c,32) | var %nums $puttok(%nums,$+(%i,:,%b),%c,32) | inc %i | inc %c } | echo -a time: $+ $calc($ticks - %ticks) $+ ms-> %nums
 
Valid codepoints are 1 through 65535:
//var %i 10000 , %s , %low 9100 , %high $calc(%low +999) | while (%i) { var %a $rand($chr(%low),$chr(%high)) | if (%a !isincs %s) var %s %s %a | dec %i } | echo -a $numtok(%s,32) tokens: $sorttok(%s,32)

Notes

  • Does not matter which value is higher, $rand(a,z) is same as $rand(z,a)
  • If either value is not 0 or positive, the first character of both value is considered the starting/ending codepoint.
  • If one of the numbers is negative, it's treated as if the value was the hyphen's codepoint 45.
  • If either number has a fraction, it's rounded 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(foo,bar) returns codepoints from the first character of each value, 'b' through 'f'

Compatibility

Added: mIRC v2.1a
Added on: 28 Feb 1995
Note: Unless otherwise stated, this was the date of original functionality.
Further enhancements may have been made in later versions.


See also

[Expand]
v · d · e mIRC identifier list