MZ [Solved] Random Variable Range

I'm working on a custom battle system, and I'm looking to make an attack that deals damage between some random numbers. I have the base attack value that I'd like to base these numbers on stored in variable 30. Currently, I'm trying to store this new random damage value into a new variable, using 4 different variables: the range, minimum damage, maximum damage, and final attack power.

Here's what I have so far (using the built-in Control Variables events):

JavaScript:
Control Variables : #0030 ATK Power = 100;
Control Variables : #0057 ATK Range = $gameVariables.value(30) / 5;
Control Variables : #0058 ATK Max = $gameVariables.value(30) + $gameVariables.value(57);
Control Variables : #0059 ATK Min = $gameVariables.value(30) - $gameVariables.value(57);
Control Variables : #0060 ATK Active = Math.floor(Math.random() * $gameVariables.value(58)) + $gameVariables.value(59);

I plan to eventually change that value of 100 to a different variable, but kept it at that for now, for testing purposes.
In this case, this attack should damage between 80 - 120, but right now it seems to be picking a random number between 80 to 160. I suspect it's that last line in my code that's doubling the max value somehow, but I'm still learning JavaScript, so I'm not entirely sure how this is happening.

I also tried to use the example for the Control Variables in the RMMV Script, but after a few tries, I wasn't able to get that working either.

I feel like I'm getting really close here, but does anyone have any ideas on how I can update my code to make this work?
you doing this in a very overcomplicated way. just use the variance under damage formula.
v(30) with a variance of 20%
if variable 30 is 100, 20% of 100 is 20. meaning the attack will deal random between 80 and 120 damage.
100-20 is minimum damage. 100+20 is maximum damage.

if you change the variable to say 80. the variance will change along with it.
80*20%=16 so attack would deal 64-96 damage
For the record, what it's currently doing is this:

Setting variable "ATK Power" to 100
Setting variable "ATK Range" to 100 / 5, which comes out to 20.
Setting variable "ATK Max" to 100 + 20, or 120.
Setting variable "ATK Min" to 100 - 20, or 80.
Setting variable "ATK Active" to a random number from 0 to 0.999... multiplied by ATK Max, plus ATK Min.

If your random number is 0, you'll have 0 + 80 = 80.
Here's the problem: let's say the random number is 0.5; you'll end up with 60 + 80 = 140 damage.
if your random number is 0.99, you'll have 0.99x max + min, which will give you 118 + 80 = 198 damage. That's obviously not what you want.

What you want is Math.random() * (max - min) + min

But yeah seriously like Tiamat said just put 20% variance on the skill. 😛
Thank you both for your quick replies!

I got it to work with updating to this code thanks to your suggestion, @Trihan, so I appreciate the breakdown:
JavaScript:
Control Variables : #0060 ATK Active = Math.floor(Math.random() * ($gameVariables.value(58) - $gameVariables.value(59)) + $gameVariables.value(59));

And sorry, just wanted to clarify - I'm creating this outside of the built-in RPG Maker battles, through events and variables (not using the built-in skills or anything), so that's why I'm using variables rather than the built-in battle formulas. I'm not sure if the variance in damage formula only applies in battles or if it can be used in any event, but did just want to put that out there just in case!

I actually haven't worked with the built-in battles much, so I'm not super familiar with how those formulas work, but if there is a more efficient way to do this, I would be curious about how that works. If you have any suggestions on where I could learn more about it, I'd be interested, too! Thanks again for your help!
You actually could still utilise it if you're using the standard actor/enemy classes. Create a new instance of Game_Action, call setSkill with the ID of your skill that has v[30] in the formula box, then you can just call makeDamageValue passing in your enemy as the target and it'll calculate the variance for you. The other benefit is that it does all the other weakness/resistance calculations for you as well.

Original thread: https://forums.rpgmakerweb.com/threads/solved-random-variable-range.132341/