1

**Scripting and Reverse Engineering / Re: [FF7] 3D Battler RNG**

« **on:**2022-11-14 19:42:54 »

The linked calculator page gave the formula for calculating the probability so I wrote a quick JavaScript script to execute in the console

Code: [Select]

`//probability of succeeding a roll (win, loss)`

function prob(w, l) {

return 1/(w+l)*w;

}

function binomial(n, k) {

let coeff = 1;

for (let x = n-k+1; x <= n; x++) coeff *= x;

for (x = 1; x <= k; x++) coeff /= x;

return coeff;

}

//probability of rolling r number of successes at p probablity with n number of dice

function P(n, r, p) {

return binomial(n,r) * Math.pow(p,r) * Math.pow(1-p,n-r)

};

//probability of rolling /at least/ r at p with n

function Pplus(n, r, p) {

let out = 0;

for(let i = r; i <= n; i++) {

out += P(n, i, p);

}

return out;

}

function battler() {

let opponents = [

[120, 62],

[ 87, 83],

[ 64,128],

[ 64,192]

];

let rolls = 19;

let wins = 10;

let output = [];

for(let [i, stack] = [0, 1]; i < opponents.length; i++) {

let p = prob(...opponents[i]);

let success = Pplus(rolls, wins, p);

stack *= success;

output[i] = [success*100, stack*100];

}

return output;

}

console.log(...battler());