$rand replacement script

From Scriptwiki
Jump to: navigation, search

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