archcorsair / YourGamblingAddiction

Gambling Addiction Discord Bot (A BoxBot companion bot)
MIT License
0 stars 1 forks source link

Add probability of win % #17

Open Arcanemagus opened 6 years ago

Arcanemagus commented 6 years ago

Given n wins and m losses, the following will calculate the probability of that outcome: image

Arcanemagus commented 6 years ago

Going from https://stackoverflow.com/questions/3959211/fast-factorial-function-in-javascript there are a few different solutions to this:

Lanczos Approximation

function factorial(op) {
  // Lanczos Approximation of the Gamma Function
  // As described in Numerical Recipes in C (2nd ed. Cambridge University Press, 1992)
  const z = op + 1;
  const p = [
    1.000000000190015,
    76.18009172947146,
    -86.50532032941677,
    24.01409824083091,
    -1.231739572450155,
    1.208650973866179E-3,
    -5.395239384953E-6
  ];

  const d1 = Math.sqrt(2 * Math.PI) / z;

  let d2 = p[0];
  for (let i = 1; i <= 6; ++i) {
    d2 += p[i] / (z + i);
  }

  const d3 = Math.pow((z + 5.5), (z + 0.5));
  const d4 = Math.exp(-(z + 5.5));

  return d1 * d2 * d3 * d4;
}

Recursive

function rFact(num) {
  if (num === 0) {
    return 1;
  }
  return num * rFact(num - 1);
}

Iterative

function sFact(num) {
  let rval = 1;
  for (let i = 2; i <= num; i++) {
    rval = rval * i;
  }
  return rval;
}

Benchmarking the three shows that the recursive method is ~90% slower than the fastest, the iterative is ~15% slower, and the approximation is the fastest method. As the numbers get larger the difference gets larger and larger. Since we don't really care if the value is off by a tiny bit the approximation will work just fine for us.

Arcanemagus commented 6 years ago

image 😆

Arcanemagus commented 6 years ago

Those are actually all returning NaN, we'd have to use some library to handle numbers that large.