# Method for putting variance directly in Damage Formula?

#### Damascus7

##### Veteran Hi there! I'm making a skill that drains HP and MP at the same time, which is requiring me to use a somewhat complex equation. For now the formula is marked as HP Drain in the dropdown box, and has this formula:

Code:
``m = Math.min(a.mat - b.mdf * 2, b.mp); a.gainMp(m); b.gainMp(-m); a.atk * 6 - b.def * 2``
It does drain both correctly, the one problem I'm having is that the variance on the skill only applies to the HP drain part of the formula. Is there a way to put in a variance for the MP Drain as part of the formula?

For example, without variance, a given use of the Skill does exactly 24 MP Drain. I want it to instead have a 20% variance and do between 20 and 27 MP Drain. I tried using math formulas to do it, but since the formula only seems to handle integers, I tried multiplying the percent variance by 100 and then divide the whole thing by 100 afterward. It ended up looking like this:

Code:
``m = Math.round((Math.min((a.mat - b.mdf * 2) * (Math.floor(Math.random() * 140) + 80)) / 100, b.mp)); a.gainMp(m); b.gainMp(-m); a.atk * 6 - b.def * 2``
For the example given above, instead of landing between 20 and 27 like I want, it does 184 without any variance. So either my math is way off, or I'm not using the formulas correctly. Could anyone help me with this?

#### bgillisp

##### Global Moderators If you want it to drain between 20 and 27 damage, you would replace m with 20 + some command which gives a random number between 0 and 7. That will make it between 20 - 27.

#### Damascus7

##### Veteran No, I'm only using that as an example for comparison between what the attack would do with or without variance. It's not doing a set amount of damage, but a.mat - b.mdf * 2, and I just want it to calculate for a 20% variance.

#### bgillisp

##### Global Moderators I see now. The issue was indeed the math. I think you want * 40 instead, not 140, as * 140 is increasing it by 140%, not 40% like you want, as the variance is 40% of the total.

#### Damascus7

##### Veteran Thanks for that correction! I also realized I was accidentally using math.min instead of math.floor, and that i don't really need to use math.round. It still was not working properly, but then I ALSO realized that I had misplaced a parentheses or two in the math functions and adjusted accordingly. My new formula is:

Code:
``m = Math.floor(((a.mat - b.mdf * 2) * (Math.floor(Math.random() * 40) + 80)) / 100), b.mp; a.gainMp(m); b.gainMp(-m); a.atk * 6 - b.def * 2``
I also realized that as long as I have Math.floor at the end, I don't need to worry about decimals, so I simplified it further:

Code:
``m = Math.floor((a.mat - b.mdf * 2) * (Math.random() * 0.4 + 0.8)), b.mp; a.gainMp(m); b.gainMp(-m); a.atk * 6 - b.def * 2``
This seems to be working! I've tested it several times and the MP drain seems to be within 20% variance of expected values.

I can summarize this below for anyone else who needs help with this.

Where the original skill formula = [FORM], and the variance you want is x (30% = 0.3), your damage formula will be as follows:

Code:
``Math.floor([FORM] * (Math.random() * (2x) + (1-x))``
Thanks for your help! I guess I'm good at math, but bad at syntax.

Last edited:

### Latest Profile Posts

Debugging... ♪
Government : "Hehehe~ I will spread lies that the earth is round so that I could sell globes~"
Anyone here managed to never burn out on programming? My current limit is 3 months(6-8 hours per day, 7 days per week, 50 hour programming week on average), and then I just have to take a break, sometimes lasting few days(with at most 2 hours on programming per day). I've been always like this so far, so I admire those who can always be so passionate on programming This is starting to become a momochi collection y'all!
Meet Swefo The candy Eater! Hide your candy... I or I sweefe sweefefooo~~~
Automatic level scaling without an option to turn it off can be a very bad choice for a game. The worst I've ever seen was in Nightmare of Druaga, where if a power outage made your system turn off, the game detects it as an attempted cheat and overwrites your save while taking away your best gear, leaving you unable to progress at the late stages of the game, and unable to recover.