# Gw Temp

## Tutorial - 'Dynamically Ranged Random Numbers' by Rast

An item about RPGMaker 2000 posted on

### Blurb

A look at some advanced tecniques in random numbering.

### Body

So, you're thinking "Why do we need a tutorial on how to make random numbers in RM2K? It already has that built-in!". Well, the answer is simple. RM2K's random number generator works fine if you know the low and high end your random range in advance, but if you're using any kind of a custom system, the chances are better than not that if it needs any kind of a random number (such as for weapon or skill variances), the low and high end of the ranges will be sitting in variables and won't be known in advance.

This tutorial is broken into two parts. The first part shows you how to generate a random number between two variable-defined values, and the second part shows you how to generate a random number based on a single variable value and a percentage variance variable.

Part 1 - Random number between two variable values

Create a common event that expects the low and high end of your random range to be in two variables, and will return the final value in a 3rd variable.

Now for the fun part! The way this will work is to find the value of 1% of the difference between the low and high values, multiply this value between a random number between 1 and 100, and add this to the low end of the range. We do this in the following manner:

1) Multiply the low and high end of the range by 100. We do this because RM2K uses integer math and if the values are below 100 it won't work. We will divide the final result by 100 to get back to our original range.

2) Load a temporary variable with the high end of the range, subtract from this variable the low end of the range, and divide the result by 100. The temporary variable now has a value equal to 1% of the range between the low and high values.

3) Multiply your temporary variable by a random number between 1 and 100.

4) Add the final value to the low end of the range.

5) Divide the low end of the range by 100. Load your result variable with the value of the low end of the range, and you're done.

As you can see, this method will generate a random number between two variable values in 1% increments. This works fine for virtually everything, but if you need a higher resolution, you can get it by changing all the 100's above to a higher value (preferably a power of 10).

Method 2 - Variable value with a variable variance percentage

Basically, this method is for those times when you need to do this calculation:

[Variable] +/- [Variable]%

This method works by finding a random variance percentage in the range given, then applying that change to the variable. It uses this method:

1) Load a working variable with your variance change amount and muliply it by 100. RM2K uses integer math and can't handle small percentage values.

2) Take a temporary variable and assign it a random value between 0 and 200. This is to generate the actual variance percentage later. Values betwenn 0 and 99 will end up being negative modifiers, 101-200 positive modifiers, and 100 will leave the value unchanged.

3) Multiply this temporary variable by the variance change amount (NOT the x100 working value).

4) Subract this temporary variable from the working variable you made in step 1.

Before we go on... yes, I know this 4 step math trick doesn't look like it's made any sense so far, but.. you now have the x100 percentage change to apply sitting in your working variable.

What we've done is to take your variance amount and put it in the same x100 format we used in method 1, so RM2K can deal with it. When we made the 0-200 random variable, we were creating a random variance that was between +/- 1%, with 100 being the center point where the value doesn't change. When we multiplied it by the variance amount, we were putting it in the same scale as the working variable we made in step 1, and then we subtracted this value from the working value in step 1 to get our final percentage change, still in the x100 format. This value is centered around the working value created in step 1, so values lower than this become negative percent modifiers, and values above it become positive percent modifiers. If this still doesn't make any sense, run the numbers yourself on paper a few times, and you'll see what's going on quickly enough.

5) Divide the working variable by 100 to get the final percent change. We lose some resolution here but it makes the remaining calculations much easier.

6) Store in your temporary variable the value of the variable being changed. Multiply this variable by 100. At this point, you have the x100 value of the variable being changed sitting in your temporary variable, which makes the original exactly 1% of this value, so...

7) Multiply the original (not x100) variable being changed by the final percentage change we calculated earlier that should still be sitting in your working variable.

8) Add this value to the x100 value sitting in your temporarly variable. Remember, adding a negative number will make the value smaller.

9) Load the original variable of the value being changed with the x100 temporary variable.

10) Divide the original variable of the value being changed by 100. You're done.

As an alternative to this 10-step method, you could just calculate the low and high end of the percentage variance ranges and use the first method to find the final value, but the second method is faster (which is important in CBS's, card games, or other places where you have to generate a lot of random numbers at once). Use either as it suits you.