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 22: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

v · d · e mIRC identifier list

$ $$, $, $0, $1-, $!, $&, $*, $+, $++, $?

A $abook, $abs, $acos, $active, $activecid, $activewid, $adate, $address, $addtok, $addtokcs, $agent, $agentname, $agentstat, $agentver, $alias, $and, $anick, $ansi2mirc, $aop, $appactive, $appstate, $asc, $asctime, $asin, $atan, $atan2, $auto, $avoice, $away, $awaymsg, $awaytime

B $banlist, $banmask, $base, $beta, $bfind, $bindip, $bitoff, $biton, $bits, $bnick, $bvar, $bytes

C $calc, $caller, $cancel, $cb, $cd, $ceil, $chan, $chanmodes, $channel, $chantypes, $chat, $chr, $cid, $clevel, $click, $cmdbox, $cmdline, $cnick, $color, $colour, $com, $comcall, $comchan, $comchar, $comerr, $compact, $compress, $comval, $cos, $cosh, $count, $countcs, $cr, $crc, $creq, $crlf, $ctime, $ctimer, $ctrlenter

D $date, $day, $daylight, $dbuh, $dbuw, $dccignore, $dccport, $dde, $ddename, $debug, $decode, $decompress, $deltok, $devent, $dialog, $did, $didreg, $didtok, $didwm, $dir, $disk, $dlevel, $dll, $dllcall, $dname, $dns, $donotdisturb, $dqwindow, $duration

E $ebeeps, $editbox, $email, $emailaddr, $encode, $envvar, $error, $eval, $evalnext, $event, $eventid, $eventparms, $exists, $exiting

F $false, $feof, $ferr, $fgetc, $file, $filename, $filtered, $find, $finddir, $finddirn, $findfile, $findfilen, $findtok, $findtokcs, $fline, $flinen, $floor, $font, $fopen, $fread, $fromeditbox, $fserv, $fserve, $fulladdress, $fulldate, $fullname, $fullscreen

G $get, $getdir, $getdot, $gettok, $gmt, $group

H $halted, $hash, $height, $hfile, $hfind, $hget, $highlight, $hmac $hmatch, $hnick, $host, $hotline, $hotlinepos, $hotlink, $hotp, $hregex, $hypot

I $iaddress, $ial, $ialchan, $ibl, $idle, $iel, $ifmatch, $ifmatch2, $ignore, $iif, $iil, $inellipse, $ini, $initopic, $inmidi, $inmode, $inmp3, $inpaste, $inpoly, $input, $inrect, $inroundrect, $insong, $insongpause, $instok, $int, $intersect, $inwave, $inwho, $ip, $iptype, $iql, $isadmin, $isalias, $isbit, $isdde, $isdir, $isfile, $isid, $islower, $istok, $istokcs, $isupper, $isutf

K $keychar, $keyrpt, $keyval, $knick

L $lactive, $lactivecid, $lactivewid, $left, $leftwin, $leftwincid, $leftwinwid, $len, $level, $lf, $line, $lines, $link, $lock, $locked, $lof, $log, $log10, $logdir, $logstamp, $logstampfmt, $longfn, $longip, $lower, $ltimer

M $maddress, $mask, $matchkey, $matchtok, $matchtokcs, $maxlenl, $maxlenm, $maxlens, $md5, $me, $menu, $menubar, $menucontext, $menutype, $mid, $mididir, $mircdir, $mircexe, $mircini, $mkfn, $mklogfn, $mknickfn, $mnick, $mode, $modefirst, $modelast, $modespl, $mouse, $mousecx, $mousecy, $mousedx, $mousedy, $mousekey, $mouselb, $mousex, $mousey, $mousewin, $mp3, $mp3dir, $msfile, $msgstamp, $msgtags

N $N, $naddress, $network, $newnick, $nhnick, $nick, $nickmode, $no, $nofile, $nopath, $nopnick, $noqt, $not, $notags, $notify, $null, $numeric, $numtok, $nvnick

O $ok, $online, $onlineserver, $onlinetotal $onpoly, $opnick, $or, $ord, $os

P $parmN, $parms, $parseline, $parsetype, $parseutf, $passivedcc, $pi, $pic, $play, $pnick, $portable, $portfree, $pos, $poscs, $prefix, $prop, $protect, $puttok

Q $qt, $query

R $r, $raddress, $rand, $rands, $rawbytes, $rawmsg, $read, $readini, $readn, $regbr, $regerrstr, $regex, $regml, $regmlex, $regsub, $regsubex, $remote, $remove, $removecs, $remtok, $remtokcs, $replace, $replacecs, $replacex, $replacexcs, $reptok, $reptokcs, $result, $rgb, $right, $rnick, $round

S $samepath, $scid, $scon, $script, $scriptdir, $scriptline, $sdir, $send, $server, $serverip, $servertarget, $sfile, $sha1, $sha256, $sha384, $sha512, $shortfn, $show, $signal, $sin, $sinh, $site, $sline, $snick, $snicks, $snotify, $sock, $sockbr, $sockerr, $sockname, $sorttok, $sorttokcs, $sound, $speak, $sqrt, $sreq, $ssl, $sslcertsha1, $sslcertsha256, $sslcertvalid, $ssldll, $ssllibdll, $sslready, $sslversion, $starting, $status, $str, $strip, $stripped, $style, $submenu, $switchbar, $sysdir

T $tan, $tanh, $target, $tempfn, $ticks, $time, $timeout, $timer, $timestamp, $timestampfmt, $timezone, $tip, $tips, $titlebar, $token, $toolbar, $topic, $totp, $treebar, $true, $trust

U $ulevel, $ulist, $unsafe, $upper, $uptime, $url, $usermode, $utfdecode, $utfencode

V $v1, $v2, $var, $vc, $vcmd, $vcmdstat, $vcmdver, $version, $vnick, $vol

W $wavedir, $wid, $width, $wildsite, $wildtok, $wildtokcs, $window, $wrap

X $xor

Y $yes

Z $zip

= =$nick