Difference between revisions of "$rand"

From Scriptwiki
Jump to: navigation, search
(spelling)
m
 
Line 41: Line 41:
 
  }
 
  }
  
== Also See ==
+
== See Also ==
 
* [[$chr]]
 
* [[$chr]]
 
* [[$asc]]
 
* [[$asc]]
 
* [[$rand_replacement_script|An improved $rand script]]  
 
* [[$rand_replacement_script|An improved $rand script]]  
 
[[Category:Text and Number Identifiers]]
 
[[Category:Text and Number Identifiers]]

Latest revision as of 10:05, 2 July 2007

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

$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: A design flaw in the way mIRC handles the $rand identifier 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 accomplish 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 characters 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 identifier. 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
}

See Also