$rand replacement script
From Scriptwiki
mIRC has an inbuilt $rand identifier but there are a few errors that can occur from using 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) { ;;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 ;; 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 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 ;; 0 you have three options, 0,1,2 so find a random number here, lets say its 0. ;; 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. ;; This gives us the random number. if ($1 < 0 && $2 >= 0) { var %i = $rand(0,$calc($2 + $abs($1))) return $calc(%i - $abs($1)) } ;;See comment above but using second parameter as the negative value. elseif ($2 < 0 && $1 >= 0) { var %i = $rand(0,$calc($1 + $abs($2))) return $calc(%i - $abs($2)) } ;;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. ;; Say we get a result of 5, all we need to do is return that value negative, so add the - infront. elseif ($1 < 0 && $2 < 0) { return - $+ $rand($abs($floor($1)),$abs($floor($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. 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 $int($iif($1 < 0,-) $+ $rand(0,$abs($1))) ;;$int() is to eliminate -0 being returned. } ;;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 |