Tuesday, October 25, 2011

c0dehammer v0.1a - Understanding Re-Rolls

Before we get started with this one I am going to recommend you check out Redbeards article on DakkaDakka on Basics of Mathhammer and go over the probabilities of rerolls, as I personally would not understand it if not for him. The calculation of figuring out the percentage is:

p + ((1 - p) * p(from now on multiplication will be represented as an *)

So to steal his example if something hits on a 4+, which is the same as saying 3/6 or 1/2 or 50% or 0.5

p + ((1 - p) * p)
1/2 + ((1 - 1/2) * 1/2)
1/2 + ((1/2) * 1/2)
1/2 + 1/4
3/4

Which is the same as saying:

p + ((1 - p) * p)
0.5 + ((1 - 0.5) * 0.5)
0.5 + ((0.5)*0.5)
0.5 + 0.25
0.75

Provided you understand that the calculations between fractions and decimals are the same is all that matters. Although understanding WHY that formula works is important, Redbeard does a very good job of it, and I am not going to repeat his work (anymore). So lets create a simple program to figure out our changed probabilities with rerolls in mind.


#include <iostream>
using namespace std;

int main()
{
      
    float x =0.0;
      
    cout << "Reroll Probability Calculator" << endl;
    cout << "# of sides that indicate a Success: ";
    cin >> x;
  
    x = x / 6.0;
    x = x + ((1 - x) * x);
    cout << "Probability: " << x << endl;
  
  
    return 0;
}   
This is a better case of where c0dehammer could come in handy, as this isnt as easily accomplished as the 216 method on a calculator. You should have a slight understanding of what is going on in this program. We know that we are using a float because we are dealing with decimals (or percentages), and that the program is asking us to enter the amount of sides that will give us a successful result. This means that a 2+ would mean 5 sides indicate a success whereas a 5+ would mean 2 sides would equal a 6. We will work on making stuff more user friendly later on. This brings us to the part you may not understand:

x = x / 6.0;
x = x + ((1 - x) * x);
 The first thing that is happening is we are taking our variable that we entered and dividing it by 6. Why are we doing this you might ask? Computers don't handle fractions very well, so by dividing the number of successful sides by six we are changing our fraction into a decimal percentage that the computer will have an easier time digesting. As we know from above, the formula works whether we are dealing with percentages, or fractions, so the next part should look very familiar. If we replace x with p, we have our reroll probability formula. And lastly we display it on the screen. Pretty easy eh? Now our next step is to augment this into our already built calculator.

No comments:

Post a Comment