Copypasta:
Code:
/* probability to deal x or greater damage in n hits with damage range a to b */
function dicecdf(x,a,b,n) {
var result = 0;
var c;
for(k=0;k<=n;k++) {
c = a*(n-k)+b*k;
if(x>=c) [b]result += choose(n,k)*Math.pow(-1,k)*Math.pow(x-c,n)[/b];
}
result = [b]1-result/Math.pow(b-a,n)/factorial(n)[/b];
if(result < 0) result = 0;
return result;
}
/* factorial function */
function factorial(x) {
var result = 1;
for(i=1;i<=x;i++) {
result *= i;
}
return result;
}
/* choose function */
function choose(a,b) {
var result = 1;
for(i=1;i<=b;i++) {
result *= (a-i+1)/i;
}
return result;
}
