From WikiChip
$rand identifier - mIRC
< mirc‎ | identifiers
Revision as of 09:49, 4 March 2018 by Ouims (talk | contribs)

$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.

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