Method for putting variance directly in Damage Formula?

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?
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.
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.
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.
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.

Original thread: https://forums.rpgmakerweb.com/threads/method-for-putting-variance-directly-in-damage-formula.122044/