Difference between revisions of "$rand replacement script"

From Scriptwiki
Jump to: navigation, search
m
(changed commands)
Line 1: Line 1:
mIRC has an inbuilt [[$rand]] identifier but there are a few errors that can occur from using that you may not expect.
+
mIRC has an inbuilt [[$rand]] identifier but there are a few errors that can occur from using $rand that you may not expect.
 
Here's a list of changes this $rand2 identifier uses or fixes.
 
Here's a list of changes this $rand2 identifier uses or fixes.
 
* Handle negative numbers.
 
* Handle negative numbers.
Line 12: Line 12:
 
   if ($1 [[isnum]] && $2 isnum) {
 
   if ($1 [[isnum]] && $2 isnum) {
 
   
 
   
     ;;If number the first parameter is negative, find the possible random numbers, for example, if you give, -1 and 1 the there is 3 possible random result
+
     ;;When one of the parameters given is negative and the other parameter is posative we have to calculate the total amount of possible
     ;;  Which is -1 0 1 so we count 0-1 plus find the absolute of -1 (which is 1, if it was -69 it would be 69) this is the amount of possible
+
     ;;  results before we can attempt to find a random number.
     ;;  results that is negative, so if we add that to the second parameter, in our example, -1 1 it would be 1 + 1, this gives us 2, which means if you include
+
    ;;To do this, we add the absolute value of the negative parameter (for -1 it would be 1 and -69 it would be 69 for example) to the positive parameter.
     ;; 0 you have three options, 0,1,2 so find a random number here, lets say its 0.
+
    ;;eg.
     ;;  After that take away the possible negative values (the absolute value of first parameter, -1) which is our example is 1, so 0 now becomes -1.
+
     ;;  To find a random number from -2 to 1 we take the absolute of -2 (using $abs) which is 2 and add that to 1, 2+1 = 3
     ;; This gives us the random number.
+
    ;;    -2,-1,0,1 which is the four possibilities we can have returned.
 +
    ;;
 +
    ;;Now we know there is four possibilities we can use the inbuilt $rand number to return a value.
 +
     ;;Although we have a running total of 3 with 4 possible out comes, this is taken in to consideration when using $rand(0,3)
 +
    ;;If we imagen $rand(0,3) returns a theoretical value of 1 we need to reduce this number by the amount of negative numbers we added before.
 +
     ;;  so we now take the theoretical 1 and remove the 2 from before. 1-2 = -1
 +
     ;;You know have the random number you are looking for.
 +
 
 +
    ;;Check $1 is negative and $2 is positive
 
     if ($1 < 0 && $2 >= 0) {
 
     if ($1 < 0 && $2 >= 0) {
 +
      ;;set %i to a random number between 0 and the total of $2 + the absolute value of $1
 
       [[var]] %i = $rand(0,[[$calc]]($2 + [[$abs]]($1)))
 
       [[var]] %i = $rand(0,[[$calc]]($2 + [[$abs]]($1)))
 +
      ;;return the random number music the absolute value of $1
 
       return $calc(%i - $abs($1))
 
       return $calc(%i - $abs($1))
 
     }
 
     }
 
   
 
   
     ;;See comment above but using second parameter as the negative value.
+
     ;;Exactly the same as the above except $2 being negative and $1 being the positive parameter
 
     [[If-Then-Else|elseif]] ($2 < 0 && $1 >= 0) {
 
     [[If-Then-Else|elseif]] ($2 < 0 && $1 >= 0) {
 
       var %i = $rand(0,$calc($1 + $abs($2)))
 
       var %i = $rand(0,$calc($1 + $abs($2)))
Line 29: Line 39:
 
     }
 
     }
 
   
 
   
     ;;If both numbers are negative, find the absolute value of the parameters, say -4 -6 would give you, 4 and 6, find a random number here.
+
     ;;If both parameters are negative, find the absolute value of the parameters and find a random number between those.
     ;; Say we get a result of 5, all we need to do is return that value negative, so add the - infront.
+
     ;;Once we have the random number we can just return a negative value of the result from $rand.
     elseif ($1 < 0 && $2 < 0) { return - $+ $rand($abs([[$floor]]($1)),$abs($floor($2))) }
+
     elseif ($1 < 0 && $2 < 0) { return - $+ $rand($abs($1),$abs($2)) }
 
   
 
   
 
     ;;If we get here both numbers are possitive numbers, just return default rand values.
 
     ;;If we get here both numbers are possitive numbers, just return default rand values.
Line 39: Line 49:
 
   ;;If either or both of the parameters are not a numerical value, get the ascii value of both parameters find a random number inbetween and return the
 
   ;;If either or both of the parameters are not a numerical value, get the ascii value of both parameters find a random number inbetween and return the
 
   ;;  charater that the random number represents.
 
   ;;  charater that the random number represents.
 +
  ;;This is to stop a bug in $rand which returns $null if you try and get a random char between a alpha and a numeric.
 +
  ;;To get around this, we get the $asc value of the charectors find a random number between those two numbers and return the charector.
 
   elseif ($1 && $2) {
 
   elseif ($1 && $2) {
 
     return $chr($rand($asc($1),$asc($2)))
 
     return $chr($rand($asc($1),$asc($2)))
Line 45: Line 57:
 
   ;;If we dont have a second parameter but the first one is a number, take it that the first parameter given should be the second and first is 0.
 
   ;;If we dont have a second parameter but the first one is a number, take it that the first parameter given should be the second and first is 0.
 
   elseif ($1 isnum) {
 
   elseif ($1 isnum) {
     return $int($iif($1 < 0,-) $+ $rand(0,$abs($1))) ;;$int() is to eliminate -0 being returned.
+
     return $iif($1 < 0,-) $+ $rand(0,$abs($1))
 
   }
 
   }
 
   
 
   

Revision as of 13:42, 27 April 2006

mIRC has an inbuilt $rand identifier but there are a few errors that can occur from using $rand that you may not expect. Here's a list of changes this $rand2 identifier uses or fixes.

  • Handle negative numbers.
  • Fixes bug with mixture of numerical and alpha characters.
  • If you pass only one parameter, it treats it as if you want to find a range between 0 and first parameter.
alias rand2 {

  ;;Make sure $rand2 is called and not /rand2
  if (!$isid) { return $null }

  ;;check both $1 and $2 is a number to handle negative numbers and numbers with more then 1 char in them.
  if ($1 isnum && $2 isnum) {

    ;;When one of the parameters given is negative and the other parameter is posative we have to calculate the total amount of possible
    ;;  results before we can attempt to find a random number.
    ;;To do this, we add the absolute value of the negative parameter (for -1 it would be 1 and -69 it would be 69 for example) to the positive parameter.
    ;;eg. 
    ;;  To find a random number from -2 to 1 we take the absolute of -2 (using $abs) which is 2 and add that to 1, 2+1 = 3
    ;;    -2,-1,0,1 which is the four possibilities we can have returned.
    ;;
    ;;Now we know there is four possibilities we can use the inbuilt $rand number to return a value.
    ;;Although we have a running total of 3 with 4 possible out comes, this is taken in to consideration when using $rand(0,3)
    ;;If we imagen $rand(0,3) returns a theoretical value of 1 we need to reduce this number by the amount of negative numbers we added before.
    ;;  so we now take the theoretical 1 and remove the 2 from before. 1-2 = -1
    ;;You know have the random number you are looking for.
    ;;Check $1 is negative and $2 is positive
    if ($1 < 0 && $2 >= 0) {
      ;;set %i to a random number between 0 and the total of $2 + the absolute value of $1
      var %i = $rand(0,$calc($2 + $abs($1)))
      ;;return the random number music the absolute value of $1
      return $calc(%i - $abs($1))
    }

    ;;Exactly the same as the above except $2 being negative and $1 being the positive parameter
    elseif ($2 < 0 && $1 >= 0) {
      var %i = $rand(0,$calc($1 + $abs($2)))
      return $calc(%i - $abs($2))
    }

    ;;If both parameters are negative, find the absolute value of the parameters and find a random number between those.
    ;;Once we have the random number we can just return a negative value of the result from $rand.
    elseif ($1 < 0 && $2 < 0) { return - $+ $rand($abs($1),$abs($2)) }

    ;;If we get here both numbers are possitive numbers, just return default rand values.
    else { return $rand($1,$2) }
  }

  ;;If either or both of the parameters are not a numerical value, get the ascii value of both parameters find a random number inbetween and return the
  ;;  charater that the random number represents.
  ;;This is to stop a bug in $rand which returns $null if you try and get a random char between a alpha and a numeric.
  ;;To get around this, we get the $asc value of the charectors find a random number between those two numbers and return the charector.
  elseif ($1 && $2) {
    return $chr($rand($asc($1),$asc($2)))
  }

  ;;If we dont have a second parameter but the first one is a number, take it that the first parameter given should be the second and first is 0.
  elseif ($1 isnum) {
    return $iif($1 < 0,-) $+ $rand(0,$abs($1))
  }

  ;;We must not of been given two parameters or if only one given, must not of been a numerical. Return null.
  return $null
}
Contributed by Albie