Gw Temp


Tutorial - 'Random Numbers and Their Uses' by LinkMaster Sab

An item about RPGMaker 2000 posted on


This tutorial teaches you how to use random numbers in a few ways.


Tutorial for RPGMAKER 2003
Random Numbers and Their Uses
Random numbers in Rm2k/3 are semi-limited, as they must be within a specific range that can't be varied. However, there are a number of things you can do with random numbers that are very easy to use. Randomness is a nice relief to your general boring monotonous events, and can add some spicy elements to them. In this tutorial, I will cover how to use random numbers to your advantage, as they are very powerful if you know what you're doing with them.

A note as well, this tutorial starts a new format for me to help you understand what's going on. Variable and switch names will be highlighted in blue, and code will be in tables with monospace type ("courier"), along with light gray comments in italics next to commands. Main topics are bolded. I hope you can use these additions to your advantage.

Random Numbers in General
Using random numbers isn't a hard thing to do. Set a variable to a random number between two values, and it sets it to a random whole number between the said values.

Fortunately, they're versatile as well. You can use them for a number of things. Here's your sample code for a random number from 1 to 5 set the variable Random:
<> Variable Oper: [Random] Set, Rnd [1-5] Chooses a number between 1 and 5.

Percentage-Based Roll
This is what you use to determine whether or not, for example, a 13% chance is successful or not. It requires just two variables: One to set to a random number between 1 and 100, which I call Random, and one to set the range of success (such as the 13% chance mentioned earlier, Chance.

To perform a percentage-based roll, set Random to a random within the range 1 to 100. Then set Chance to the chance of success. For example, if you wish to say that the roll has a 13% chance of succeeding, then set this variable to 13. If you are to have any bonuses or penalties, apply them to Chance. In the example, I'll make each level of the character will add an additional 1% to the chance.

Now that you're done initializing the variables, you must check to see if the roll (made when you set Random, sneaky eh?) was successful or not. Set up a branch with an else condition, and check to see if Random is less then or equal to Chance. If it is, then it was successful. If not, it was a failure. In my example, I'll tack in some messages, but I'm sure you can think of something much more productive then that.

That wraps it up for the random percentage-based roll, so here is my example:
<> Variable Oper: [Random] Set, Rnd [1-100] Makes the random roll itself.
<> Variable Oper: [Chance] Set, 13 The 13% chance is set.
<> Variable Oper: [Chance] +, Zack Level Here's my 1% per level bonus.
<> Branch if Var [Random] is V[Chance] Less/Equal Check if the roll is successful.
  <> Message: You were a success!
: Else
  <> Message: You were a failure!
: End

"For Loops" and Random Numbers
"For loops" are very helpful for programmers, and can be utilized with a little trouble in RPGMaker 2000/3. They go through commands a certain number of times and then exits it.

To make a "for loop", you'll need a variable (which I'll call Loop). Set it to 0. Then start a loop. At the beginning of the loop, check to see if your variable is set to the number of loops, and if it is, then break the loop. After that check, you can perform whatever you want to repeat in the loop. You can put in a message to check out your loop, and to see how it works. Then close it off and add 1 to your Loop variable:
<> Variable Oper: [Loop] Set, 0 Sets your loop count to 0.
<> Loop
  <> Branch if Var [Loop] is 5 Sees if you've reached your number of loops, here 5.
    <> Break Loop Break your loop.
  : End
<> Message: Loop #\V[Loop]! A message just to test the loop.
<> Variable Oper: [Loop] +, 1 Make sure the loop number increments here.
: End Loop

You may be wondering how this ties in with random numbers. Imagine that same loop, but with a random number. In this case, you can set it to loop a random number of times between two numbers. This is useful for random wait loops, used to make random intervals in parallel processes. I use it in my thunderstorm code.

To do this, you'll need a second variable, which I'll call Random. Before your loop starts, add in Random, and set it to a random number between whichever suits your needs. In my example, I'll use 3 to 10. Next, when you check to see if Loop is equal to the number of loops, instead check if its equal to our new variable Random. Then you can proceed as normal. In this example I replaced my message with a wait to show you the random interval:
<> Variable Oper: [Loop] Set, 0 Sets your loop count to 0.
<> Variable Oper: [Random] Set, Rnd [3-10] Sets up your random number of loops.
<> Loop
  <> Branch if Var [Loop] is V[Random] Equal Sees if you've reached your random number of loops.
    <> Break Loop Break your loop.
  : End
<> Wait: 1.0 Sec This is here for your random time interval.
<> Variable Oper: [Loop] +, 1 Make sure the loop number increments here.
: End Loop

Simulating the Damage Algorithm
While most of the time you'll want the battle system to take care of calculations, but then there are times when you want to figure out damage, hit rates, evasions and escapes on your own. First you must calculate the base values, then apply random number techniques. This is the most complicated part of the tutorial, so be warned.

This calculation is of the normal attack damage. This is: Damage (HP decrease value) = (Attack power of Attacker / 2) - (Defense power of Defender / 4). The actual damage value varies from 80% to 120% of this base value. First of all, you'll need three variables to calculate the base value: Attack, Defense, and Result. Set Attack equal to the attacker's Attack rating and divide it by 2, then set Defense to the defender's Defense rating and divide it be 4. Set Result to Attack, then subtract Defense. You'll now have the base value. From this base value, multiply Result by a random number from 80 to 120, and then divide it by 100. This is your damage
<> Variable Oper: [Attack] Set, Zack Attack
<> Variable Oper: [Attack] /, 2
<> Variable Oper: [Defense] Set, 1:Slime Defense
<> Variable Oper: [Defense] /, 4
<> Variable Oper: [Result] Set, Var [Attack]'s Value
<> Variable Oper: [Result] -, Var [Defense]'s Value
<> Variable Oper: [Result] *, Rnd [80-120] Multiply it by a random number between 80 and 120.
<> Variable Oper: [Result] /, 100 Divides it by 100 to complete the percent increase.

There are plenty of other applications for random numbers, so just keep thinking about it. If you need some help with it, as always, you can come to me.

~ LinkMaster Sab