MV Math.random Output

Hello,

I am currently working on a skill that will roll a random number between 1-6 and apply a state based on the number rolled. Below is what I'm using and it seems to work most of the time, but occasionally there is a number rolled that won't apply a state. I'm not entirely sure how to check the random number output or what I need to adjust here. Any help would be appreciated!


<After Eval>

var roll = Math.floor(Math.random()*(6)+1)

if(roll==1){

target.addState(353);

}

if(roll==2){

target.addState(354);

}

if(roll==3){

target.addState(355);

}

if(roll==4){

target.addState(356);

}

if(roll==5){

target.addState(357);

}

if(roll==6){

target.addState(358);

}


</After Eval>

Thanks!
Moved to: Plugin Support

var roll = Math.floor(Math.random()*(6)+1)

It shouldn't make a difference, but try Math.floor(Math.random() * 6) + 1

If you want to see the output you might be able to add a console.log(roll) after that line, then press F8 while playing and go to the Console tab to see what the resulting number is.
I'm not entirely sure how to check the random number output
console.log is generally a good way to go for this type of thing.

JavaScript:
<After Eval>

var roll = Math.floor(Math.random()*(6)+1);

console.log("The value of roll is: " + roll);

// The rest of your stuff

</After Eval>

While playtesting the game, press F8 to open the console, and then your log messages will show up in there as they are executed.
A few pieces of advice for you:
- Any time you're posting any kind of code stuff, do it inside CODE tags (the </> icon above the text box). That will preserve your indenting and anything inside brackets.

- There's no purpose to the parentheses around the number 6 after your Math.random. It won't make anything malfunction, but it's also not doing anything.

- if/else conditions, loops and similar things automatically apply to the next single code statement. You only need to create a code block with braces if there's more than one statement within. It isn't making anything break, but you could be simplifying it to:
Code:
if (roll==1)
    target.addState(353);

- You should be using if/else if statements. By listing everything with if, the code for each statement is getting checked even if one of them already passed and applied a state. That won't make anything malfunction in this specific case, but it's unnecessary and generally a bad habit. You could also use a switch statement, which would allow you to get rid of the extra variable.

- For that matter, with the way your state IDs are numbered, there's no reason for you to have any if or else or switch statements. They're all sequential, so your entire notetag could be simplified to:
Code:
<After Eval>
target.addState(353+Math.floor(Math.random()*6));
</After Eval>

If you're still getting situations where states aren't getting added there, then you know there's something wrong where the target is resistant to the state or the state isn't filled out properly or something.
Thank you all for the assistance! With the console log, I was able to figure out there was an issue when the same number rolled twice in a row.
Yes, by default the engine will do nothing with a state if the target already has it. You could use a plugin to change that (add the duration, increase the effect, something) or make your notetag code more complex to always ensure it results in a state that the target doesn't have.

Original thread: https://forums.rpgmakerweb.com/threads/math-random-output.142529/