Difference between revisions of "$rand"

From Scriptwiki
Jump to: navigation, search
(Added comment)
m (added reference to the improved $rand script)
Line 46: Line 46:
 
* [[$chr]]
 
* [[$chr]]
 
* [[$asc]]
 
* [[$asc]]
 +
* [[$rand_replacement_script|An improved $rand script]]
 
[[Category:Text and Number Identifiers]]
 
[[Category:Text and Number Identifiers]]

Revision as of 14:42, 10 November 2005

Returns a random value depending upon what values you give the identifer.

$rand(v1,v2)

If v1 and v2 is a numerical value, a number from v1 to v2 is returned. If both parameters v1 and v2 are not a numerical value both parameters are taken as their ascii value and the random result is returned as a character.


Note:

If v1 or v2 are not both given as a numerical or non-numerical mIRC will error out.
A design floor in the way mIRC handles the $rand identifer means you can not give v1 a value of 1 and v2 a value of A and expect a result of a character between ascii value 49 and 65.
To accoumplish this, please check the examples below.

Also note, if you do not give a parameter as an integer the value is rounded up.

$rand can not handle negative numbers.


Examples

$rand(1,100) ;; Returns a value between 1 and 100.
$rand(A,Z)   ;; Returns a character between A and Z
$rand(A,z)   ;; Returns a character between A and z

Note: the above example returns A-Z and a-z and also, [\]^_` which are the charaters between Z and a. Characters 91-96.


var %i = One,Two,Words Three and Four,Five
var %num = $numtok(%i,44)     ;; Returns 4 since there is for tokens delimited by a comma $chr(44).
var %rand = $rand(1,%num)     ;; Returns a random number from 1 to 4.
echo -ag $gettok(%i,%rand,44) ;; Returns a random token from %i.


The Example below handles situations where a numerical and a non-numerical

alias rand2 {
  ;;Make sure $rand2 is called and not /rand2
  if (!$isid) { return $null }
  else { echo $color(Info) -atg rand alias can only be used as an identifer. ex: $rand2(1,10) | return }

  ;;Make sure we have two parameters.
  if ($1 && $2) { return $chr($rand($asc($1),$asc($2))) } ;;Gets the ascii value off $1 and $2 then gets a random value between those and returns its $chr()

  ;;If $1 or $2 is null return nothing.
  return $null
}

Also See