Equation to determine outcome of event

samiam41
samiam41 used Ask the Experts™
on
Hey Experts!  Fully admit that I am working on something that is slightly outside my realm but I'm very interested in learning how to make this work so your help is greatly appreciated.

For software being developed, I'm trying to build a formula to calculate the success of an event (build a wooden crate) based on factors like character's smarts (+10% increase in success), speed (+5% increase in success) and ability (+15% increase in success).  

I've been looking all over the internet for how to build this equation or how to determine success/failure.  I've written formulas for Excel but not something like this so I appreciate your help and time.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
Well you didn't specify a language to use but, we could write pseudo or lang of our choice I suppose.

 The larger question you need to answer is what do YOU want the formula to look like?

Is it purely chance?
Is it calculated each time tried?
Do # of tries make a difference in chance of success?
What are the total chances of success?

how do the modifiers stack?

To give an example:

For generating a random number, total chances could be calculated as out of 100% or any arbirary number.

in simplest terms you might as a person has a 1 in 100 chance of success.

So using this pseudo PS Code:

IF ( Rand(99) -eq 0) { "SUCCESS!"}

Open in new window


So you have a function "RAND" which you give a rnad max to, and it returns a random number between 0 and 99 (100 chances), if we rolled a 0 we are successful (I chose this because in most languages the Random number generator includes 0, and in most languages 0 is "FALSE" and in many languages you can check for "NOT FALSE" which is the quickest executing way of checking the result as:)

IF ( ! Rand(99) ) { "SUCCESS!"}

Open in new window


This can be defined as a 1%, 1/100, or 1 in 100 chance


But how do you r modifiers modify that?
  are they multiplicative?
 Additive?
  is it possible to have or exceed 100%?

Lets say you have the normal equation

$X = Percent Chance
  • $X = 1 = 100% Chance  X= 0 = 0% Chance
  • $X = 0.1 = 10% Chance - 1/10, or 1 in 10
  • $X = 0.01 = 1% Chance - 1/100, or 1 in 100

Now what is the BASE % Chance.  It is not going to be 0, or if it is you have to write SPECIAL code to check and handle that, which you we'll avoid as it's unlikely to be nessessary.

Lets say for argument purposes you have a 10 percent chance 1/10 or 0.1

$X= 0.1

We can modify our Existing test for Success mightily easily if we want to use this!

IF ( Rand( ( (1/$X)-1) ) -eq 0) { "SUCCESS!"}

Open in new window


Wait Q, what's all this then?

( (1/$X)-1)

Its a method to set the chances to whatever you like.


1/0.1 = 10
but 0 to 10 is  11 chances


so we must subtract 1 so the function checks from 0 to 9 for 10 chances
.

1/0.01 = 100
-1 is 99
0 to 99 is 100 chances

1/0.025  ?  (2.5% chance) is 4 -1 is 3
0 to 4 is 5 chances, so again -1 = 3  and the function will return {0,1,2,3} and if we find 0 we have a success!
byundtMechanical Engineer
Most Valuable Expert 2013
Top Expert 2013

Commented:
The problem you describe seems like a good fit for a product type of formula, perhaps with a base probability and a limit:
Probability of success = Minimum(100% or Smarts * Speed * Ability*Base probability of success)

You will need to establish ranges for each of the parameters, such as 25% to 200%.

The Base probability of success is what you want the likelihood of outcome to be when every character attribute is 100%. Let's say it is 52%. Your character will succeed 100% of time if Smarts = 125%, Speed = 110% and Ability = 140%.

The reason for the Minimum function is in case (Smarts * Speed * Ability*Base probability of success) exceeds 100%. If you want there to be a small chance that the character will fail even if all attributes are maxed out, then change the 100% in the minimum to say 90% .

Author

Commented:
Love working with you.

That's part of the design aspect that I'm working on.  I would believe the modifiers would stack (increase and decrease) as they apply (player has 3 traits that increase while 1 that decreases the chance for success).

Is it calculated each time tried?  Yes.  It would be a new "instance" when it is attempted.

Do # of tries make a difference in chance of success? Not necessarily.  Once it's attempted, successful completion builds crate, failed attempt destroys material.
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

byundtMechanical Engineer
Most Valuable Expert 2013
Top Expert 2013

Commented:
Once you have determined the desired probability of success, you can apply it to your software using a random number generator:
If (random number between 0 and 1) >= probability of success then the task was completed successfully, otherwise, it failed.

Author

Commented:
@byundt, I think you're on the right track!  Working through some of that now as I do want a small factor to fail as chance.  

So how would I write that in the document?  Something along the lines of, if the total value of the chance is equal to or less than 95%, deem it successful?  Setting the benchmark is part of the task I'm guessing.
byundtMechanical Engineer
Most Valuable Expert 2013
Top Expert 2013
Commented:
I goofed in stating how the element of chance is introduced.

If your calculated probability of success using character attributes is 90%, then you compare that value to a uniformly distributed random number chosen between 0 and 1. If the random number is 0.1 or higher, then success occurred. Otherwise, failure.

If  (random number between 0 and 1) >= (100% - probability of success) then attempt succeeded, otherwise failed

Author

Commented:
@byundt, brilliant!  It makes sense and I'm working through it now as it clicks in my head.  Let me come back with some intelligent questions.
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
Okay so we have this really easy way to check for success given a particular chance of success:



Now, for your modifiers, how will they apply?

  Do they add a flat % chance?
  or do they modify the total percent chance by some value?
  do they come all in a row?  do they stack?  do you use the highest one?

Those are choice you have to decide. but I can show some examples here too.

IE if you want to make them additive and already have them defined as %s of 100:

factors like character's smarts (+10% increase in success), speed (+5% increase in success) and ability (+15% increase in success).  

Lets assume they are additive for now thats the "simple" scenario

You're probably tracking these traits as whole numbers:
$Smarts_Lvl=10
$Speed_Lvl=5
$Ability_Lvl=15

Open in new window


Lets assume for now that you just want them all to apply equally to the chances of success as you've already told me the % chances of each one:

$Smarts_Chance=(100/$Smarts_Lvl)
$Speed_Chance=(100/$Speed_Lvl)
$Ability_Chance=(100/$Ability_Lvl)

Open in new window


Before we go any further lets address this, in our original example we only cared about the chance X which we said was the Base Chance

IF ( Rand( ( (1/$X)-1) ) -eq 0) { "SUCCESS!"}

Open in new window


But really X is the "Total Chance" so lets rewrite this to have:

$Total_Chance

IF ( Rand( ( ( 1 / $Total_Chance ) - 1) ) -eq 0) { "SUCCESS!"}

Open in new window


And we'll call the Base Chance $Base_chance instead of X.

$Base_chance = 0.1

So we can Sum these up:
$Total_Chance = ( $Base_chance, $Smarts_Chance, $Speed_Chance, $Ability_Chance | Measure-Object -sum ).sum

Open in new window


And then check our success
IF ( Rand( ( ( 1 / $Total_Chance ) - 1) ) -eq 0) { "SUCCESS!"}

Open in new window

Lead SaaS Infrastructure Engineer
Commented:
Nice!  I was about to write out the example in CMD, but I found Powershell random function already does the -1 for you which means I can simplify the code even further for the example.

 in Powershell the Get_random function doesn't need the -1 done (it does it for you! Unlike CMD!)  so I was about to write this up ad a batch example but now I'm sticking with a quick powershell.

 In oversimplifying my initial equations I forgot that it's impractical to have non integer numbers.


So I would ammed slightly as well.


$Percent_Success_Base=10
$Percent_Success_Max=90

[PSCustomObject]$Character=@{
Smarts=10
Speed=5
Ability=15}

#Sum the Percent Chances and set a max value.
$Percent_Success_Total = [math]::Min((($Character.Values | Measure-Object -sum).sum + $Percent_Success_Base ),$Percent_Success_Max)

for ($J=1; $J -lt 13; $J++) {
    $Rand=Get-Random -Maximum ( 100 ) # So we we can output the results
    IF ( $Rand -lt $Percent_Success_Total) {
        Write-Host "SUCCESS!  - $Rand is Less than $Percent_Success_Total" -ForeGroundColor Green
    } ELSE {
        Write-Host "FAILURE!  - $Rand is More than $Percent_Success_Total" -ForeGroundColor Red
    }
}

Open in new window

Author

Commented:
Didn't abandon this question.  Working it through my app while also working the day job.  Thanks for your patience.

Author

Commented:
Thanks Experts!  With the start of Win 10 and Server 2016 migration project, my extra time has dried up.  I apologize for not getting back to you all sooner but can't thank you enough for all your help and time.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial