PSA: How to design chance to hit

Started by AngleWyrm, April 03, 2017, 10:32:17 AM

Previous topic - Next topic

AngleWyrm

Target audience: This post requires some basic understanding of algebra
I've tried to minimize the math and maximize the thought stream; any improvements welcome.


Building a new weapon? Or a machine that can fail? Or a test for a loot drop? This is a how-to on dancing with Lady Luck.

It isn't as simple as an average because an average only works over a long haul. But there's a method to the madness that works for any luck-based test for success, and it's accuracy and the length of the previously mentioned long-haul are both factors in the solution.

The Formula
Maybe we know a toHit chance and want to know how many times it will take to not miss with the whole burst.
numberOfShots = log(burstFailure)/log(chanceToMiss)
Chance of Failure
Turns out it's much more convenient to think in terms of failure than success for this type of problem. So we might want a series of shots to completely fail only once in a while. Lets call that burstFailure. The more shots we fire in a burst the less likely it is for all of them to miss, which we're calling burstFailure.

If we know the chance to hit then we also know the chance to miss (100% - chanceToHit). So we can be precise about the chance of having a burstFailure event:
burstFailure = chanceToMiss x chanceToMiss x chanceToMiss... with one chanceToMiss for each shot, which can be stated more simply as
burstFailure = chanceToMiss^numberOfShots

So now we got a juicy new toy burstFailure, what can we do with it?

Let's say we want the burst failure outcome to happen only once out of every twenty times we fire a burst; 1/20 = 5% chance of burstFailure. We know that as we increase the number of shots fired in a burst the chance of them all missing goes down. And we want it to go down to our chosen 1 in 20 chance of them all missing. So how many shots is that?





burstFailure = chanceToMiss^numberOfShotsformula we worked out earlier
1/20 = chanceToMiss^numberOfShotssubstituting our chosen value for burstFailure to happen about 1 time out of every 20
log(1/20) = log(chanceToMiss) x numberOfShotstaking the log of both sides of an equation
log(1/20)/log(chanceToMiss) = numberOfShotsdividing both sides of an equation by log(chanceToMiss)
So now we have nice and tidy formula that takes as input the chanceToMiss of a single shot and returns the number of shots needed to reach our desired burstFailure rate.

My 5-point rating system: Yay, Kay, Meh, Erm, Bleh

The-Eroks

Great feedback... I feel like this is something that I wanted to know, but never knew that I wanted to know it. I will say that I was disappointed at the end of your tutorial though...  how many shots was it in your example? What a cliff hanger ;)

Maybe include some real numbers or example to illustrate your point?

AngleWyrm

#2

An example brought to life

Let's say we have our favorite Space Chicken Launcher with the Homing Chickens mod installed. At our current range of ten chicken-flaps, four out of five of our avian harbringers of death and destruction will likely strike their target. So we have made an 80% chanceToHit, which also means we made a 20% chanceToMiss.

A brief aside on Confidence levels
Previously on the animal channel, we decided to say only once in twenty should a burst of fire completely miss all shots. This is technically called a confidence level of how certain we wish to be of hitting. At one end of the spectrum we can be 100% certain and at the other end we have no certainty. Interestingly enough both of these ends have something to offer.

If I roll a pair of six-sided dice and add them together, I can be 100% certain the outcome will be a number between 2 and 12. The merit of this is that it shows us the full range of what is possible. As I lower my confidence, or increase my willingness to be wrong some of the time, I can narrow my answer down to what is probable.

And now, back to the show
We have chosen to allow an oops once out of twenty runs, so an error may happen 5% of the time. In this way we can say we are 95% confident in our answer.

These two decisions -- chanceToMiss and burstFailure -- are all that is needed to use our formula



numberOfShots = log(burstFailure)/log(chanceToMiss)Our secret recipe, from the vault at Salsa & Salsa...
numberOfShots = log(1/20)/log(1/5)substituting burstFailure = 1/20 and chanceToMiss = 1/5
numberOfShots = 1.8614 shots

So our amazingly awesome Space Chicken Launcher with the Homing Chickens mod requires only two shots to meet our high standards of excellence.

A way to cross-check this is to say 20% chanceToMiss x 20% chanceToMiss = 4% chance to miss both shots, which is below our 5% threshold.
My 5-point rating system: Yay, Kay, Meh, Erm, Bleh

AngleWyrm

#3

What if I want to design the number of shots it takes

Doctor Theopolis emerged from the Lab-O-Doom with a dismembered hand, and proposed attaching a magazine-fed pea shooter to the pirate recuperating in his facility. He had some design parameters in mind:

  • It's a rapid fire weapon
  • Not particularly accurate
How do we translate this desire into an implementation? As an arbitrary and artistic choice, we decide that maybe a batch of half a dozen shots is fired and they all miss their target once out of every three times.

And we'd like a function that returns the chanceToHit when given our design parameters.



burstFailure = chanceToMiss^numberOfShotsGiven
1/3 = chanceToMiss^numberOfShotssubstituting our chosen burstFailure of one in three bursts
1/3 = chanceToMiss^6substituting our chosen batch size of half a dozen shots

We've got it down to our one desired variable, so it's an equation with a single scalar-value result. Pretty much. Anyway, moving on from the realm of imaginary numbers...

Our desired chanceToMiss, which will lead directly to chanceToHit, is trapped behind the magic field of an exponent.

Can anyone cast the ancient incantation necessary to bring it out?
My 5-point rating system: Yay, Kay, Meh, Erm, Bleh

AngleWyrm

#4

Ok, I'll do it
We have an equation that looks like so: 1/3 = chanceToMiss6 and the chanceToMiss has an exponent of 6
This just won't do. So how can we get that power of six to be a power of one? It's pretty simple: Divide the power by six. On both sides, Daniel-san.



( 1/3 )1/6 = chanceToMiss6/6raising both sides to the power of 1/6
0.8327 = chanceToMissjust whipping out the ole calculator
chanceToHit = 1 - 0.8327 = 0.1673 = 17%chanceToHit = 1 - chanceToMiss
My 5-point rating system: Yay, Kay, Meh, Erm, Bleh

Granitecosmos

You can choose to either do the math behind the post-processing curve and stat weighings, along with the accuracy^numberoftiles with the different accuracy values for x% of total range....

...Or just be glad someone already did this for you and use this.

AngleWyrm

#6

Need some accompaniment?
Do be do be doowaaaaah
My 5-point rating system: Yay, Kay, Meh, Erm, Bleh

jimthenoob

I wont lie, i used tons of tools like the one posted when I was taking civil engineering in school. Its still an asset to know the nuts and bolts of how they get the numbers, good post.

AngleWyrm


Was kinda hoping someone would ask what a fractional exponent means.

But thanks for the complement.
My 5-point rating system: Yay, Kay, Meh, Erm, Bleh


AngleWyrm


Glad you asked!
It's called a root.

someVariable3 is just a simpler way of saying someVariable x someVariable x someVariable
So 43 = 4 x 4 x 4 = 64.

This operation can be reversed, to travel back in time and space, to do the time warp again....
641/3 = (something) x (something) x (something) = 4

We're most familiar with square roots and somewhat with cube roots.
32 = 3x3 = 9, and the reverse: 91/2 = (something)x(something)  = 3
so we can see that 91/2 is the same thing as the square root of 9

23 = 2x2x2 = 8, and going the other way, 81/3 = (something)x(something)x(something) = 2
so we can see that 81/3 is the same thing as the cube root of 8
My 5-point rating system: Yay, Kay, Meh, Erm, Bleh

AngleWyrm

#11

Quote from: jimthenoob on April 06, 2017, 03:11:17 PM
I wont lie, i used tons of tools like the one posted when I was taking civil engineering in school. Its still an asset to know the nuts and bolts of how they get the numbers, good post.

There are two questions that gamers regularly express interest in
  • How many times do I have to do this?
  • Seems I've done this a bunch, shouldn't I be there yet?
Both of these questions are variables in a single formula, and because it is a mathematical expression it can be swapped around to solve for either one.


How many times do I have to do this?


set confidence level to 95%
numberOfTimes = log(1 - confidence) / log(1 - chanceOfSuccess)

Example: Some RNG process has a 16% chance of success
numberOfTimes = log(1 - 0.95) / log(1 - 0.16)
numberOfTimes = 17.2 tries to achieve 95% confidence in getting at least one success.


How sure am I? Shouldn't I be there yet?


set numberOfTimes to what has already been done
confidence = 1 - (1 - chanceOfSuccess)numberOfTimes

Example: I have already run 3 tries of an RNG process that has a 16% chance of success
confidence = 1 - (1 - 0.16)3
confidence = 41% certain that I should already have at least one success.


Game developers are in a position that gamers rarely have occasion to experience; they have a need to look at that same equation from the third possible perspective: They have an idea of the number of times something should happen, and the confidence with which they want that to be true. What they want to calculate is the chance of success given those two variables. This perspective also comes up in scenarios where data is being tested to determine the chance of success.


What is the chance of success?


set numberOfTimes to what is desired
chanceOfSuccess = 1 - (1 - confidence)1/numberOfTimes

Example: I want it to take up to 5 tries before they're 95% certain to have it
chanceOfSuccess = 1 - (1 - 0.95)1/5
chanceOfSuccess = 45% chance to succeed on any given try.
My 5-point rating system: Yay, Kay, Meh, Erm, Bleh