From Final Fantasy Hacktics Wiki
(Redirected from Event Instruction BF)
Jump to navigation Jump to search
This page is about the event instruction. For ASM random, see idk fill this in later.

{BF} Random


Calls the RNG routine and stores the resulting number in a given variable.

This command can actually generate a negative number, not just a positive one. See below for how to fix that.

Variable : Byte (hex)

Warning: Only x00-x7F (words) are valid with this instruction!

Click here for the list of Variables

Modulus : Half-Word (unsigned)

Divides the resulting random number by this Modulus value and keeps the remainder.

For example: A modulus of 100 will return random values between 0 and 99.

If this value is set to 0, it will keep the full 4 bytes of the randomly generated number and store it into the variable as is.

Avoiding negative results

To put it simply (and perhaps a bit inaccurately), 7FFFFFFF in binary is 01111111 11111111 11111111 11111111, and that first bit - the zero - is what determines whether or not the value is negative. Any binary value above that spills over into 0x80000000 range, which is the negative value flag. ANDVar merges the bits of the values of two variables in a specific way - it will only keep bits as 1 if both are already set to 1. This means that the only effect of merging a value of 7FFFFFFF with another value through ANDVar is that it will turn a negative value positive: it will turn off the negative bit every time, but then will keep every other enabled bit of the value, while disabling the superfluous enabled bits of the 7FFFFFFF value.

So once you generate a Random value, you'll want to merge it with a register of 7FFFFFFF in order to force it to be a positive value.

Set up your register on a variable. In this case, we'll say it was on x0003.

LUI(x0003,x7FFF) OR(x0003,xFFFF)

Set up your random value on another variable. We'll say this one is on x0002.


Merge their bits with ANDVar. Remember, the first variable in commands like these is always where the output will go.


And just like that, your random value is forced to be a positive number.

For anyone less experienced with programming or binary, a more straightforward solution might be to use that second variable to generate a value of -1 to multiply your random value by, or copy your random value to it and subtract it twice, but both of these solutions would require an If check to find out if your random value was negative in the first place. This is simpler and takes up less event space.

Generating specific random number ranges

The easiest way to bring the Random value down to the range you might want would be to divide the random value by the range, then check the remainder. Dividing 3 by 7, for example, gives a remainder of 3. So does dividing 10 by 7, or 17 by 7... etc. No matter how small or large your random value is, it will always have a perfectly balanced random chance of giving you any of the potential remainders within that range. The only exception to this is if the random value is negative, but you can read the section above for how to force the value to be a positive one.

DIVHI is the command used for dividing two values and getting the remainder. Since we used a value of 7 in the above example, we'll go ahead and use it here. We'll assume that the random value has been stored to Var x0002 and had its negative flag disabled, as per the previous section.


Or, if you want to use a dynamic number range, you'll store that number to another variable earlier on, then use DIVHIVar when the time comes. We'll say it was Var x0004.


[other event stuff]