Difference between revisions of "$rand"

From Scriptwiki
Jump to: navigation, search
m (added reference to the improved $rand script)
m
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
Returns a random value depending upon what values you give the identifer.
+
Returns a random value depending upon what values you give the identifier.
  
 
  $rand(v1,v2)
 
  $rand(v1,v2)
Line 8: Line 8:
  
 
'''''Note''''':
 
'''''Note''''':
If ''v1'' or ''v2'' are not both given as a numerical or non-numerical mIRC will error out.
+
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.
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 accomplish this, please check the examples below.
To accoumplish this, please check the examples below.
+
 
+
Also note, if you do not give a parameter as an integer the value is [[$ceil|rounded up]].
Also note, if you do not give a parameter as an integer the value is [[$ceil|rounded up]].
+
$rand can not handle negative numbers.
 
$rand can not handle negative numbers.
 
  
  
Line 21: Line 19:
 
  $rand(A,Z)  ;; Returns a character between ''A'' and ''Z''
 
  $rand(A,Z)  ;; Returns a character between ''A'' and ''Z''
 
  $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.
+
''Note'': the above example returns A-Z and a-z and also, [\]^_` which are the characters between Z and a. Characters 91-96.
  
  
Line 34: Line 32:
 
   ;;Make sure $rand2 is called and not /rand2
 
   ;;Make sure $rand2 is called and not /rand2
 
   [[if]] (![[$isid]]) { return [[$null]] }
 
   [[if]] (![[$isid]]) { return [[$null]] }
   [[If-Then-Else|else]] { echo [[$color]](Info) -atg rand alias can only be used as an identifer. ex: $rand2(1,10) | [[return]] }
+
   [[If-Then-Else|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.
 
   ;;Make sure we have two parameters.
Line 43: 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 11: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