Trying to figure out the script to add a state with a chance

I've figured out to have a skill add a state if the user is under another state, but it makes it happening 100% of the time when I only want it happening at 20%.

This is what i got that is working.

Code:
if (user.isStateAffected(x)) {
 target.addState(y);
}

What am I missing?
Code:
if (user.isStateAffected(x) && Math.random() < 0.2) {
 target.addState(y);
}

Note that this chance will not be affected by LUK.
Thanks James. It worked.
Ok, I've been messing around with this to increase the chance that the state lands depending on the state the actor has.

This is what I got:
Code:
<Pre-Damage Eval>
if (user.isStateAffected(x) && Math.random() < 0.05) {
  target.addState(n);
} else {
if (user.isStateAffected(y) && Math.random() < 0.1) {
  target.addState(n);
} else {
if (user.isStateAffected(z) && Math.random() < 0.2) {
  target.addState(n);
} else {
if (Math.random() < 0) {
  target.addState(n);
}}
</Pre-Damage Eval>

I don't know if it's correct or if I'm just unlucky enough that I keep getting bad rolls when testing.
And before anyone asks I've figured out how to remove state x if y is in place and both x & y if z is in place.

I also tried with a skill that does it with multiple states:
Code:
<Pre-Damage Eval>
if (user.isStateAffected(x) && Math.random() < 0.05) {
  target.addState(n);
} else {
if (user.isStateAffected(y) && Math.random() < 0.1) {
  target.addState(n);
} else {
if (user.isStateAffected(z) && Math.random() < 0.2) {
  target.addState(n);
} else {
if (Math.random() < 0) {
  target.addState(n);
}}

if (user.isStateAffected(a) && Math.random() < 0.03) {
  target.addState(o);
} else {
if (user.isStateAffected(b) && Math.random() < 0.05) {
  target.addState(o);
} else {
if (user.isStateAffected(c) && Math.random() < 0.09) {
  target.addState(o);
} else {
if (Math.random() < 0.01) {
  target.addState(o);
}}
</Pre-Damage Eval>

Is my math correct?
I think you may want to check for syntax errors. Make sure there is enough { or }.
Thanks again James.

Original thread: https://forums.rpgmakerweb.com/threads/trying-to-figure-out-the-script-to-add-a-state-with-a-chance.121880/