How do I set then use a random number in a skill?

I put together the following formula:


if ($gameVariables.value(1) > b.mdf) {$gameVariables.value(1) + a.atk - b.def} else {a.atk - b.def}

I also have it run a common event that sets variable 1 to Math.randomInt(6) + 1

Unfortunately the event is run after damage calculation.

How do I set a random number which is later referenced in the same skill?
Set it before the formula. This has the added advantage of not requiring a game variable to do:

JavaScript:
const rand = Math.randomInt(6) + 1; rand > b.mdf ? rand + a.atk - b.def : a.atk - b.def
Thank you, sir.

Am I able to reference rand in the message for the skill?
It would be convenient to see the result of the roll immediately rather than have to math it out.
Maybe something like this?

Damage formula:
JavaScript:
v[12] = Math.randomInt(6) + 1; v[12] > b.mdf ? v[12] + a.atk - b.def : a.atk - b.def

Skill message:
Code:
%1 uses %2 (\v[12])

Replace 12 with the number of a variable you'll use to temporarily store the value, and change the skill message however you need. I don't know if this approach will work without plugins (might need a plugin that parses text codes in skill messages). I'm also not sure if this will work for multi-hit skills (either Repeats is greater than 1 or Scope is not a single target).
Maybe something like this?

Damage formula:
JavaScript:
v[12] = Math.randomInt(6) + 1; v[12] > b.mdf ? v[12] + a.atk - b.def : a.atk - b.def

Skill message:
Code:
%1 uses %2 (\v[12])

Replace 12 with the number of a variable you'll use to temporarily store the value, and change the skill message however you need. I don't know if this approach will work without plugins (might need a plugin that parses text codes in skill messages). I'm also not sure if this will work for multi-hit skills (either Repeats is greater than 1 or Scope is not a single target).
The variable text code works in skill use messages without a plugin, so this will work.
Maybe something like this?

Damage formula:
JavaScript:
v[12] = Math.randomInt(6) + 1; v[12] > b.mdf ? v[12] + a.atk - b.def : a.atk - b.def

Skill message:
Code:
%1 uses %2 (\v[12])

Replace 12 with the number of a variable you'll use to temporarily store the value, and change the skill message however you need. I don't know if this approach will work without plugins (might need a plugin that parses text codes in skill messages). I'm also not sure if this will work for multi-hit skills (either Repeats is greater than 1 or Scope is not a single target).

Interestingly when done this way the message will briefly show the old value then change it to the new value.
This does work for my purposes though.
Thank you, kind sirs.
Interestingly when done this way the message will briefly show the old value then change it to the new value.
This does work for my purposes though.
Thank you, kind sirs.
I suspect that's possibly because the skill use message gets processed before the formula does, so it doesn't update the value until after it's already showing.

Original thread: https://forums.rpgmakerweb.com/threads/how-do-i-set-then-use-a-random-number-in-a-skill.181753/