public class Spell {
// chance = (0, 100>
private int chance;
public void execute() {
if (chance < 100) {
/*
chance:80
random:40 ... if(true) ... succeed
chance:80
random:80 ... if (true) ... failed
chance:80
random:81 ... if (true) ... failed
chance:79
random:80 ... if (false) ... succeed
chance:66
random:<0,65> (66 numbers) ... if (false) ... succeed
random:<66,99> (34 numbers) ... if (true) ... failed
*/
if (chance <= (int) (Math.random() * 100)) {
System.err.println("failed");
return;
}
}
System.out.println("succeed");
}
//Test
public static void main(String[] args) {
Spell spell = new Spell();
spell.chance = 80;
spell.execute();
}
}
Is this correct way to calculate chance? I need certain things to happen chance% of times. I wonder whether I did some mistake here or whether it works.
Let's say we have chance = 80, I need it to succeed in 80% of times and fail in 20% of time. There are souts for that in code, where I will add a function later.